1

I have a StreamReader reading a file like below for reading a .csv file:

using (StreamReader sr = new StreamReader(filePath, System.Text.Encoding.GetEncoding("iso-8859-1")))
{
    string line = string.Empty;
    line = sr.ReadLine()
}

The first line of the file is: å ä ö Ü (for testing purposes). However, when I try to read the file C# does not read the characters as it should:

enter image description here

Any idea why that is?

KGB91
  • 630
  • 2
  • 6
  • 24
  • 3
    What's the encoding of the file? – madreflection Jan 21 '22 at 22:54
  • It is a plain .csv file. Does such a file have an encoding? I did not consider that. – KGB91 Jan 21 '22 at 22:55
  • 2
    The string you got matches what you would get if you take "å ä ö Ü" encoded in UTF-8 (which is a likely candidate for how the CSV file is encoded), and then decode it as iso-8859-1 – harold Jan 21 '22 at 22:56
  • Yeah but can csv-files have an encoding? I did not know that. – KGB91 Jan 21 '22 at 22:56
  • 6
    All text must have an encoding. Otherwise it would be raw bytes. An encoding is what gives meaning to those bytes. – harold Jan 21 '22 at 22:57
  • Okay, I have exported it from C# via `using (var textWriter = new StreamWriter(filePath, true))` so you are probably right. How do I solve this? – KGB91 Jan 21 '22 at 22:58
  • 2
    https://stackoverflow.com/questions/8151379/forcing-streamwriter-to-change-encoding – Nikki9696 Jan 21 '22 at 22:59
  • I see and you are right - thanks I did not know that. Can I resave it with notepad++ to a proper formate? – KGB91 Jan 21 '22 at 23:00
  • Windows? https://support.microsoft.com/en-us/office/choose-text-encoding-when-you-open-and-save-files-60d59c21-88b5-4006-831c-d536d42fd861 – Nikki9696 Jan 21 '22 at 23:01
  • 1
    You could just read it real quick and resave it too. That's probably what I'd do :) – Nikki9696 Jan 21 '22 at 23:04
  • Yes, once I manged to resave the file problem was solved. A huge thanks for that. I did not think about that when I saved the file via C#... Learned something new today. – KGB91 Jan 21 '22 at 23:04
  • Wong encoding, it is UTF8. – Hans Passant Jan 21 '22 at 23:08
  • 3
    Worth reading: https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ – Sir Rufo Jan 22 '22 at 00:50
  • 1
    @KGB91 obviously all text files have encodings. *There's no such thing as plain text files*. Read [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) – phuclv Jan 22 '22 at 01:58

1 Answers1

1

Remove encoding or select the right one. I am using this syntax without any problem

    string str = string.Empty;
    using (StreamReader r = new StreamReader(@"C:\..."))
        str = r.ReadToEnd();

ouput

å ä ö Ü
Serge
  • 40,935
  • 4
  • 18
  • 45