1

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 !

4
  • First convert your file to UTF-8, then use that encoding Commented Oct 2, 2015 at 20:24
  • Possible duplicate of C# Help reading foreign characters using StreamReader Commented Oct 2, 2015 at 20:25
  • For GetEncoding(string) the argument must be a so-called WebName, i.e. an IANA-registered encoding name such as Windows-1252. Commented Oct 2, 2015 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"))). Commented Oct 2, 2015 at 20:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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. :(
Finally, investigating from your link, I found the following thread : stackoverflow.com/questions/14110730/… with the following answer that works for me : stackoverflow.com/a/14110867/2762990 ! I am going to post the solution as an answer :)
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();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.