XmlException: The data at the root level is invalid. Line 1, position 1.
This might be a really obscure (or dumb?) ASP.NET / C# problem, but again I thought I'd post something about it in case it helps someone else searching about this issue....
I was getting an exception when loading an XML file:
Error with ... xml\nav.xml: System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReader.ParseRoot() at System.Xml.XmlTextReader.Read() at System.Xml.XmlValidatingReader.ReadWithCollectTextToken() at System.Xml.XmlValidatingReader.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.LoadXml(String xml)
Using the following code:
Searching for others' advice / solutions for such a problem on the web didn't help very much... it kept happening no matter what I was trying.
Turned out the problem was very very simple to fix (as they sometimes are) - it was just a typo. The variable "xml" was a full file path to an xml FILE I was trying to load, and silly me, you have to use docXml.Load() to load an xml FILE, not docXml.LoadXml() - the typo must have gotten in there from a poor use of VS.NET Intellisense or something (auto-completing LoadXml instead of just Load) - doh!
Changing the code to:
Worked just fine (of course).
Like I mentioned, just in case this helps someone else!