XmlException: Data At The Root Level Is Invalid
Friday, April 18th, 2008A few days ago I needed to write some functionality to fetch an XML document from a URL and load it into an XmlDocument. As always I use the WebClient to retrieve simple documents over HTTP and it looked like this:
using (WebClient client = new WebClient())
{
string xml = client.DownloadString(“http://example.com/doc.xml”);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
}
I ran the function and got this very informative XmlException message: Data at the root level is invalid. Line 1, position 1. I’ve seen this error before so I knew immediately what the problem was. The XML document that was retrieved from the web had three strange characters in the very beginning of the document. It looks like this:
<?xml version=”1.0″ encoding=”utf-8″?>


