As stated above, what's the difference between XElement.Load
and XDocument.Load
? They both seemingly load an XML file.
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.
The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.
One possible use for this method is to create a copy of a DOM document in a LINQ to XML tree. To do this, you create an XmlNodeReader from a DOM document, and then use the XmlNodeReader to create an XDocument. LINQ to XML's loading functionality is built upon XmlReader.
The XDocument class contains the information necessary for a valid XML document, which includes an XML declaration, processing instructions, and comments. You only have to create XDocument objects if you require the specific functionality provided by the XDocument class.
The difference is that an XElement
type represents an XML fragment while the XDocument
type represents an entire XML document with all associated meta-data.
That being said however, for most simple cases you can use them interchangeably.
It is important to understand the subtle differences in querying between these two types and for more information on that I would recommend that you please read Querying an XDocument vs. Querying an XElement:
When you load a document via
XDocument.Load
, you will notice that you have to write queries slightly differently than when you load viaXElement.Load
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With