XmlException: The data at the root level is invalid….

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:

docXml.LoadXml(xml);

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:

docXml.Load(xml);

Worked just fine (of course).

Like I mentioned, just in case this helps someone else!

Comments 23

  • Hey…thanks for posting this. It helped and saved all the pain. I was using XmlSerializer.Deserialize which takes TextReader as argument. I was passing new StringReader(), i changed it to new StreamReader().

  • Thanks for this post – I had the same mistake in my code! Saved me some time, too!

  • I thank you for this post. This was driving me nuts.
    Off to the pub for a pint now.
    mb

  • You’re a champ! Thanks for the Post. I also made exactly the same mistake and was about to smash everything within a 3 metre radius.

  • Thanks dude, same problem! I had no idea wtf I was doing wrong…

  • Hi:
    I have the same problem while trying to
    copy web site from localhost to server
    the localhost site works just fine
    but in the server (with customers mode=”off”) I get:
    Unable to convert input xml file content to a DataSet. Data at the root level is invalid. Line 1, position 1.
    which is:
    (I’m using *xsd file)
    help! please!

  • Thank YOU!!! This sucked.
    v

  • And yet another Thank You!!!

  • Hey thanx for the post. It just worked fine 🙂

  • another thank you

  • I am getting this problem in BizTalk while passing an XML (a string) to a dll function which takes it as a string input. Even though I have used docXml.LoadXml(xml); it is giving me the error.
    Here is how I passed the xml to my BizTalk helper class in the orchestration:
    msgOutExtract = BizTalkTest.Helpers.LOB.removeTests(System.Convert.ToString(msgInExtract));
    On the otherhand, the same function works well if I call it from a genral assembly. Anyone any idea?

  • I had the same error trying to deserialize a class, the serialized xml was save in a db field and then extracted and deserialized usint a IO.MemoryStream and a routine to convert a string to a byte array…
    …after reading this post and simply using IO.StringReader the problem was solved !
    thanks!

  • Thank you very much, this solved a lot of headache

  • Hey man,
    U rock!!!! really cool piece of advice to the newbies like me. I think all intelligent ppl do the same mistake.

  • Nice tip. I got this error for a slightly different reason…
    turns out the default StreamReader encoding is UNICODE, but the xml docs were in UTF-8!
    danke!

  • Thank you, it helped me as well. It does not give error now, but still I cannot see the data that I want to insert to the document as well. Have you got any suggestion? Thanks.

  • yeah thanks man, that was dumb of me, sometimes can go in circles over something you should know, but thats what these postings are for I suppose! thanks again! was scratching my head all morning! 🙂

  • after doing like that ,i got this error
    The type ‘System.Xml.XmlDocument’ must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate.

  • Hey…thanks for posting this. It helped and saved all the pain. I was using XmlSerializer.Deserialize which takes TextReader as argument. I was passing new StringReader(), i changed it to new StreamReader().
    Same here, thanks for that 🙂 who thought that would be the error

  • I love you…

  • made the same mistake you did. posted on several groups for answers. got all kinds of suggestions. finally found the answer here. strange nobody else figured it out.

  • Thanks a bunch, had the same problem and you saved me some time.

  • phew, thank god this appeared in the first result on google 😉