What's the difference between XElement and XDocument?

21,718

Solution 1

XDocument represents a whole XML document. It is normally composed of a number of elements.

XElement represents an XML element (with attributes, children etc). It is part of a larger document.

Use XDocument when working with a whole XML document, XElement when working with an XML element.

For example - XElement has a HasAttributes property indicating whether any attributes exist on the element, but an XDocument doesn't, as such a property is meaningless in the context of a whole XML Document.

Solution 2

From MSDN:

Note that you only have to create XDocument objects if you require the specific functionality provided by the XDocument class. In many circumstances, you can work directly with XElement. Working directly with XElement is a simpler programming model.

XDocument derives from XContainer. Therefore, it can contain child nodes. However, XDocument objects can have only one child XElement node. This reflects the XML standard that there can be only one root element in an XML document.

Solution 3

Here's a practical example from msdn which makes it clear. Assume you have this in test.xml file:

<Root>
    <Child1>1</Child1>
    <Child2>2</Child2>
    <Child3>3</Child3>
</Root>
  1. With XDocument if you do this:

    foreach (var element in XDocument.Load("test.xml").Elements())
        Console.WriteLine(element);
    

    You get this back:

    <Root>
        <Child1>1</Child1>
        <Child2>2</Child2>
        <Child3>3</Child3>
    </Root>
    

    To get the value at Child1 node, you will have to do:

    var child1 = XDocument.Load("test.xml").Element("Root").Element("Child1").Value;
    

    Or

    var child1 = XDocument.Load("test.xml").Root.Element("Child1").Value;
    
  2. With XElement if you do this:

    foreach (var element in XElement.Load("test.xml").Elements())
        Console.WriteLine(element);
    

    You get this back:

    <Child1>1</Child1>
    <Child2>2</Child2>
    <Child3>3</Child3>
    

    To get the value at Child1 node, you will do:

    var child1 = XElement.Load("test.xml").Element("Child1").Value;
    

In short, XElement ignores the root node while XDocument doesnt. Roughly, XDocument.Root = XElement, or XDocument.Root.Elements() = XElement.Elements(). Both derive from XContainer. Another minor difference is that XElement implements IXmlSerializable which I dont think matters mostly. XElement would be enough for vast majority of cases where you just want to query the sub nodes. The name confuses me though, so I prefer to use XDocument.

Solution 4

According to the MSDN article LINQ to XML vs. DOM, under the subheading "Working Directly with XML Elements":

When using LINQ to XML, you use the XDocument class only if you want to add a comment or processing instruction at the root level of the document.

Share:
21,718
Rana
Author by

Rana

Updated on August 08, 2022

Comments

  • Rana
    Rana almost 2 years

    What is the difference between XElement and XDocument and when do you use each?

  • bluish
    bluish about 13 years
    XElement has no load method: seems incorrect, see Load(String) and other overloading Load methods.