23

I just came across with a problem using XmlDocument.LoadXml.

The application was crashing, giving the following error:

"Data at the root level is invalid. Line 1, position 1"

After inspecting the XML and finding nothing wrong with it, I googled a bit and found a tip to use XmlDocument.Load instead of XmlDocument.LoadXml.

I have tried it and it works perfectly.

My question is: What is the difference between the 2 methods and what could have cause one to work and the other to fail?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sergio
  • 8,125
  • 10
  • 46
  • 77

5 Answers5

39

XmlDocument.Load is used to load XML either from a stream, TextReader, path/URL, or XmlReader. XmlDocument.LoadXml is used to load the XML contained within a string.

They're fundamentally different ways of loading XML, depending on where the XML is actually stored. So it sounds like you were using the wrong method for where your XML is.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
26

Were you trying to use XmlDocument.LoadXml and passing in the name of a file? It doesn't do that - it assumes that the string you pass in is the XML. So you might use:

doc.LoadXml("<root><child /><root>");

or

doc.Load("myfile.xml");

If that doesn't help, could you show your failing and working code? There are different ways you could have changed from using LoadXml to Load...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I am using doc.LoadXml method. I have xml file which also contains document type definition file. I have put it the SoccerMatchPlus.dtd where the xml files exist. I got error when using doc.LoadXml method like Could not find file 'C:\Windows\system32\SoccerMatchPlus.dtd'. Why doc.LoadXml method find the file in 'C:\Windows\system32 folder? Can I change its path at runtime? – Rais Hussain Jul 26 '11 at 11:11
  • I added question with title 'Invalid character in the given encoding' kindly review my question. Kindly help me to find the solution of given problem. http://stackoverflow.com/questions/6829715/invalid-character-in-the-given-encoding – Rais Hussain Jul 26 '11 at 12:05
4

Load() loads from a certain source, whereas LoadXml() loads directly from a string

4

Assuming your using XmlDocument.Load and XmlDocument.LoadXml in the right way this problem may be caused by Byte Order Mark.

This other question might be useful.

Community
  • 1
  • 1
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • Yes, this. I just discovered, the hard way, that I shouldn't convert a `byte[]` to a `string` (even if I know the encoding) before loading an `XmlDocument`. This was the exact reason--I was getting BOM'd. I loaded the `byte[]` into a `MemoryStream` instead, and all is well. – Brian Warshaw Nov 01 '13 at 15:37
1

The application was crashing with the following error: "Data at the root level is invalid. Line 1, position 1" I suspect you xml data does not have a root level: for example:

<area id="1">
  <candidate id="0">dataata</candidate>
</area>
<area id="2">
  <candidate id="0">dataataa</candidate>
</area>

you need have at least one root level on top of the bottom levels. for example:

<areas>
  <area id="1">
    <candidate id="0">dataata</candidate>
  </area>
  <area id="2">
    <candidate id="0">dataataa</candidate>
  </area>
</areas>

so please put one mother on the top of your level, make it grand grand mother of all children

takrl
  • 6,356
  • 3
  • 60
  • 69
  • Line 1, position 1 is the very start of the document. It hasn't begun to look for structural validity and a doc-level element yet. – Brian Warshaw Nov 01 '13 at 15:34