Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlDocument.Load Vs XmlDocument.LoadXml

Tags:

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?

like image 221
Sergio Avatar asked Nov 02 '09 11:11

Sergio


People also ask

What is LoadXml?

LoadXml(XmlElement) Populates an XMLNode control with data from an XmlElement. LoadXml(XmlDocument) Populates an XMLNode control with data from the root node of the specified XmlDocument.

What's the difference between XmlDocument and XmlReader?

XmlDocument is very easy to use. Its only real drawback is that it loads the whole XML document into memory to process. Its seductively simple to use. XmlReader is a stream based reader so will keep your process memory utilization generally flatter but is more difficult to use.

What is the difference between XmlDocument and XDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.

Which method of the XmlDocument class takes XML as string while loading?

LoadXml(String) Method.


1 Answers

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.

like image 148
AdaTheDev Avatar answered Oct 14 '22 21:10

AdaTheDev