Question

I'm reading an XML file from a REST web service, parsing it, and displaying the details in a UITableView. The XML file is in encoded as iso-8859-1 and contains accented characters. If I just add the string to the tableview then I get a junk character displayed, so I've tried to convert it to UTF8 but it gets converted to a question mark, implying it doesn't understand the character.

Here's the code:

foreach(XmlNode myNode in myNodeList)
{
    Encoding isoEnc = Encoding.GetEncoding ("iso-8859-1");

    string utfResult = Encoding.UTF8.GetString (isoEnc.GetBytes(myNode.InnerText));

    _myCollection.Add(utfResult);
}

Any ideas what's going on here, and how to display the accented chars?

Was it helpful?

Solution

OK, problem now solved. It seems that my error was assuming that the StreamReader would deal with the iso-8859-1 encoding by default. I changed my StreamReader constructor from:

StreamReader reader = new StreamReader (response.GetResponseStream ());

to:

StreamReader reader = new StreamReader (response.GetResponseStream (), Encoding.GetEncoding("iso-8859-1"));

By telling the StreamReader to expect the correct encoding, everything else just falls into place.

OTHER TIPS

Well, your "conversion" to UTF-8 is highly suspicious. You're basically saying that you know better than the XML file - that although it claims to be ISO-8859-1, you really know it was encoded in UTF-8. Do you have any reason to believe that?

If you know what the characters are meant to be, I suggest you add some logging to indicate the Unicode values of those characters (as integers) and compare them with the code charts on Unicode.org. Then you'll know whether your problem is in displaying the characters, or reading them from the feed in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top