I've got a problem with the XDocument class. I am loading information of online XML API, for my first WP8.1 App, like this:
try
{
var xmlDoc = XDocument.Load(_url);
return xmlDoc;
}
catch (XmlException)
{
HttpClient http = new HttpClient();
HttpResponseMessage response = await http.GetAsync(new Uri(_url));
var webresponse = await response.Content.ReadAsStringAsync();
var content = XmlCharacterWhitelist(webresponse);
var xmlDoc = XDocument.Parse(content);
return xmlDoc;
}
But both ways are returning the wrong encoding. For example German umlauts are shown the wrong way. Every XML file I load has
xml version="1.0" encoding="utf-8"
in the top line. Any ideas?
Rather than read the data into a byte array and decoding it yourself, just read it as a stream and let XDocument.Load
detect the encoding from the data:
using (HttpClient http = new HttpClient())
{
using (var response = await http.GetAsync(new Uri(_url)))
{
using (var stream = await response.Content.ReadAsStreamAsync())
{
return XDocument.Load(stream);
}
}
}
See more on this question at Stackoverflow