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

[ Web Stuff ]
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!

Concocted on Saturday August 27, 2005 in the afternoon.



23 Comments & Trackbacks

Comment by Kushal, Oct 10, 2005 1:11 PM

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().

Comment by Tom, Oct 29, 2005 11:06 AM

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

Comment by Mike B, Nov 1, 2005 2:39 PM

I thank you for this post. This was driving me nuts.

Off to the pub for a pint now.

mb

Comment by lachlan, Nov 2, 2005 4:14 AM

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.

Comment by Christian, Nov 8, 2005 4:02 PM

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

Comment by yuval, Nov 14, 2005 5:20 AM

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!

Comment by Von, Nov 22, 2005 1:51 PM

Thank YOU!!! This sucked.

v

Comment by Jules, Nov 28, 2005 6:49 AM

And yet another Thank You!!!

Comment by Mrudula, Nov 29, 2005 9:18 PM

Hey thanx for the post. It just worked fine :)

Comment by Chris, Dec 7, 2005 1:36 PM

another thank you

Comment by Ritesh, Dec 14, 2005 3:59 AM

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?

Comment by sociabletechdk, Jan 3, 2006 5:48 AM

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!

Comment by Tom, Jan 7, 2006 12:26 PM

Thank you very much, this solved a lot of headache

Comment by Sumit Malhotra, Jan 10, 2006 6:51 AM

Hey man,

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

Comment by derK, Jan 16, 2006 12:14 AM

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!

Comment by mustafa, Feb 21, 2006 6:34 PM

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.

Comment by chris, Mar 2, 2006 10:11 AM

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

Comment by arunsai, Mar 6, 2006 2:37 AM

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.

Comment by Jos, Mar 16, 2006 6:50 AM

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

Comment by Stathis, Mar 17, 2006 3:42 AM

I love you...

Comment by cj, Mar 23, 2006 9:53 AM

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.

Comment by Nathan, Apr 20, 2006 6:40 AM

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

Comment by nexus-, Apr 23, 2006 7:11 PM

phew, thank god this appeared in the first result on google ;)


This discussion has been closed. No more comments may be added.


Email this entry to:
Your email address:

NOTE: This information is NOT collected or used for any other purpose.