Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between XElement.Load and XDocument.Load?

Tags:

c#

xml

linq

As stated above, what's the difference between XElement.Load and XDocument.Load? They both seemingly load an XML file.

like image 796
Ayyash Avatar asked Nov 04 '09 03:11

Ayyash


People also ask

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.

What is an XElement?

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.

How do I load XML in XDocument?

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.

What is XDocument C#?

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.


1 Answers

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 via XElement.Load.

like image 163
Andrew Hare Avatar answered Sep 21 '22 18:09

Andrew Hare