I need to read an XML file from the web with the encoding ISO-8859-1. After creating a XmlDocument with it I have tried to convert some InnerText of it to UTF. But that didn't work. Then I have tried to change the encoding on the HttpClient. The response string is properly formatted but when creating the XmlDocument, the app crashes with exception: HRESULT: 0xC00CE55F or with non expected characters on the XML string. How can I solve this issue?
Code Snippet:
private static async Task<string> GetResultsAsync(string uri)
{
var client = new HttpClient();
var response = await client.GetByteArrayAsync(uri);
var responseString = Encoding.GetEncoding("iso-8859-1").GetString(response, 0, response.Length - 1);
return responseString;
}
public static async Task GetPodcasts(string url)
{
var progrmas = await GetGroupAsync("prog");
HttpClient client = new HttpClient();
//Task<string> pedido = client.GetStringAsync(url);
//string res = await pedido; //Gets the string with the wrong chars, LoadXml doesn't fails
res = await GetResultsAsync(url); //Gets the string properly formatted
XmlDocument doc = new XmlDocument();
doc.LoadXml(res); //Crashes here
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//item");
//Title
var node_titles = root.SelectNodes("//item/title");
IEnumerable<string> query_titles = from nodess in node_titles select nodess.InnerText;
List<string> list_titles = query_titles.ToList();
//........
for (int i = 0; i < list_titles.Count; i++)
{
PodcastItem podcast = new PodcastItem();
string title = list_titles[i];
//First attempt to convert a field from the XmlDocument, with the wrong chars. Only replaces the bad encoding with a '?':
//Encoding iso = Encoding.GetEncoding("ISO-8859-1");
//Encoding utf8 = Encoding.UTF8;
//byte[] utfBytes = utf8.GetBytes(title);
//byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
//string msg = iso.GetString(isoBytes, 0, isoBytes.Length - 1);
PodcastItem dataItem = new PodcastItem(title + pubdate, title, link, description, "", pubdate);
progrmas.Items.Add(dataItem);
}
}

title? It's really unclear what you're trying to do. Also note thatXmlDocumentandXDocumentare different classes. If you've already converted the document into astring, it may be too late - you should give it in its original binary representation (e.g. as a Stream), and let the XML parser handle the decoding. - Jon Skeet