Search code examples
c#streamreader

StreamReader from .csv - "foreign" chars and blank values showing up as '?'


I'm having two problems with reading my .csv file with streamreader. What I'm trying to do is get the values, put them into variables which I'll be using later on, inputting the values into a browser via Selenium. Here's my code (the Console.Writeline at the end is just for debugging):

        string[] read;
        char[] seperators = { ';' };
        StreamReader sr = new StreamReader(@"C:\filename.csv", Encoding.Default, true);
        string data = sr.ReadLine();

        while((data = sr.ReadLine()) != null)
        {
            read = data.Split(seperators);
            string cpr = read[0];
            string ydelsesKode = read[1];
            string startDato = read[3];
            string stopDato = read[4];
            string leverandoer = read[5];
            string leverandoerAdd = read[6];
            Console.WriteLine(cpr + " " + ydelsesKode + " " + startDato + " " + stopDato + " " + leverandoer + " " + leverandoerAdd);
        }

The code in and of itself works just fine - but I have two problems:

  1. The file has values in Danish, which means I get åøæ, but they're showing up as '?' in console. In notepad those characters look fine.
  2. Blank values also show up as '?'. Is there any way I can turn them into a blank space so Selenium won't get "confused"? Sample output:

1372 1.1 01-10-2013 01-10-2013 Bakkev?nget - dagcenter ?

Bakkev?nget should be Bakkevænget and the final '?' should be blank (or rather, a bank space).


Solution

  • "Fixed" it by going with tab delimited unicode .txt file instead of .csv. For some reason my version of excel doesn't have the option to save in unicode .csv...

    Don't quite understand the problem of "rolling my own" parser, but maybe someday someone will take the time to explain it to me better. Still new-ish at this c# stuff...