Ivan Mihov
Ivan Mihov

Reputation: 33

How to solve encoding problem reading feed

https://sports.ultraplay.net/sportsxml?clientKey=b4dde172-4e11-43e4-b290-abdeb0ffd711&sportId=1165

I'm trying to read this feed in .NET environment and get the BOM issue (System.Xml.XmlException: 'There is no Unicode byte order mark. Cannot switch to Unicode.). How can I solve it? Is it because the xml contents doesn't have an xml declaration tag?

I tried reading the feed all of the possible ways, lets give as an example this one:

XmlReader reader = XmlReader.Create(feedUrl);
var content = XDocument.Load(reader);

Upvotes: 1

Views: 402

Answers (1)

dana
dana

Reputation: 18125

Apparently the XML Declaration seems to be throwing things off here:

<?xml version="1.0" encoding="utf-16"?>

See: Loading xml with encoding UTF 16 using XDocument

That question addresses the scenario when you have an XML File using StreamReader. Since you are downloading the file from the web, you can adapt a WebClient to a StreamReader using the OpenRead() method as follows:

string feedUrl = "https://sports.ultraplay.net/sportsxml?clientKey=b4dde172-4e11-43e4-b290-abdeb0ffd711&sportId=1165";

System.Xml.Linq.XDocument content;
using (System.Net.WebClient webClient = new System.Net.WebClient())
using (System.IO.Stream stream = webClient.OpenRead(feedUrl))
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, Encoding.UTF8))
{
    content = XDocument.Load(streamReader);
}
Console.WriteLine(content);

Strangely enough, while the document claims to be UTF-16, the HTTP response say UTF-8 which is why I am specifying that in the StreamReader constructor.

HTTP/1.1 200 OK
Date: Fri, 02 Nov 2018 16:28:46 GMT
Content-Type: application/xml; charset=utf-8

This seems to work well :)

Upvotes: 2

Related Questions