0

I am currently developing a Windows Phone 8 application in which one I have to download a CSV file from a web-service and convert data to a C# business object (I do not use a library for this part).

Download the file and convert data to a C# business object is not an issue using RestSharp.Portable, StreamReader class and MemoryStream class.

The issue I face to is about the bad encoding of the string fields.

With the library RestSharp.Portable, I retrieve the csv file content as a byte array and then convert data to string with the following code (where response is a byte array) :

using (var streamReader = new StreamReader(new MemoryStream(response)))
{
  while (streamReader.Peek() >= 0)
  {
    var csvLine = streamReader.ReadLine();
  }
}

but instead of "Jérome", my csvLine variable contains J�rome. I tried several things to obtain Jérome but without success like :

using (var streamReader = new StreamReader(new MemoryStream(response), true))

or

using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.UTF8))

When I open the CSV file with a simple notepad software like notepad++ I obtain Jérome only when the file is encoding in ANSI. But if I try the following code in C# :

using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ANSI")))

I have the following exception :

'ANSI' is not a supported encoding name.

Can someone help me to decode correctly my CSV file ?

Thank you in advance for your help or advices !

rolandl
  • 1,769
  • 1
  • 25
  • 48
  • First convert your file to UTF-8, then use that encoding – Amit Oct 02 '15 at 20:24
  • Possible duplicate of [C# Help reading foreign characters using StreamReader](http://stackoverflow.com/questions/592824/c-sharp-help-reading-foreign-characters-using-streamreader) – archon Oct 02 '15 at 20:25
  • For `GetEncoding(string)` the argument must be a so-called [WebName](https://msdn.microsoft.com/en-us/library/system.text.encoding.webname%28v=vs.110%29.aspx), i.e. an IANA-registered encoding name such as `Windows-1252`. – Alexander Obersht Oct 02 '15 at 20:29
  • @AlexanderObersht @archon @Amit : thx for your help. I finally found a working solution using `using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ISO-8859-1")))`. – rolandl Oct 02 '15 at 20:45

2 Answers2

0

You need to pick one of these.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx

If you don't know, you can try to guess it. Guessing isn't a perfect solution, per the answer here.

You can't detect the codepage, you need to be told it. You can analyse the bytes and guess it, but that can give some bizarre (sometimes amusing) results.

Community
  • 1
  • 1
Lawtonfogle
  • 974
  • 4
  • 17
  • 32
  • Thx for your answer. Unfortunately teh following code `new StreamReader(new MemoryStream(response), Encoding.GetEncoding("Windows-1252")` gives me the error : 'Windows-1252' is not a supported encoding name. :( – rolandl Oct 02 '15 at 20:35
  • Finally, investigating from your link, I found the following thread : http://stackoverflow.com/questions/14110730/reading-windows-1252-encoding-in-windows-phone-8 with the following answer that works for me : http://stackoverflow.com/a/14110867/2762990 ! I am going to post the solution as an answer :) – rolandl Oct 02 '15 at 20:44
0

From the link of Lawtonfogle I tried to use

using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("Windows-1252")))

But I had the following error :

'Windows-1252' is not a supported encoding name.

Searching why on the internet, I finally found following thread with the following answer that works for me.

So here the working solution in my case :

using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ISO-8859-1")))
{
  while (streamReader.Peek() >= 0)
  {
    var csvLine = streamReader.ReadLine();
  }
}
Community
  • 1
  • 1
rolandl
  • 1,769
  • 1
  • 25
  • 48