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 :)