Query an XDocument
vs. query an XElement
(LINQ to XML)
The query you write when you load a document via XDocument.Load differs slightly from what you write when you load via XElement.Load.
Comparison of XDocument.Load
and XElement.Load
When you load an XML document into an XElement via XElement.Load, the XElement at the root of the XML tree contains the root element of the loaded document. However, when you load the same XML document into an XDocument via XDocument.Load, the root of the tree is an XDocument node, and the root element of the loaded document is the one allowed child XElement node of the XDocument. The LINQ to XML axes operate relative to the root node.
Example: Load an XML tree using XElement.Load
, then query for child elements
This first example loads an XML tree using Load. It then queries for the child elements of the root of the tree.
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XElement.Load");
Console.WriteLine("----");
XElement doc = XElement.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XElement.Load")
Console.WriteLine("----")
Dim doc As XElement = XElement.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
This example produces the following output:
Querying tree loaded with XElement.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
Example: Load an XML tree using XDocument.Load
, then query for child elements
The next example does the same as the one above, but the XML tree has been loaded into an XDocument, rather than an XElement.
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XDocument.Load");
Console.WriteLine("----");
XDocument doc = XDocument.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XDocument.Load")
Console.WriteLine("----")
Dim doc As XDocument = XDocument.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
This example produces the following output:
Querying tree loaded with XDocument.Load
----
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>
Notice that the same query returned the one Root
node instead of the three child nodes.
One approach to dealing with this is to use the Root property before accessing the axes methods, as follows:
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XDocument.Load");
Console.WriteLine("----");
XDocument doc = XDocument.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Root.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XDocument.Load")
Console.WriteLine("----")
Dim doc As XDocument = XDocument.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Root.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
This query now performs in the same way as the query on the tree rooted in XElement.
This example produces the following output:
Querying tree loaded with XDocument.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>