How to find a list of child elements (LINQ to XML)
This article compares the XPath child elements axis to the LINQ to XML Elements axis.
Example: Find all child elements of an element
This example finds all of the child elements of the Address
element in XML document Sample XML file: Multiple purchase orders.
The XPath expression is: ./*
XDocument cpo = XDocument.Load("PurchaseOrders.xml");
XElement po = cpo.Root.Element("PurchaseOrder").Element("Address");
// LINQ to XML query
IEnumerable<XElement> list1 = po.Elements();
// XPath expression
IEnumerable<XElement> list2 = po.XPathSelectElements("./*");
if (list1.Count() == list2.Count() &&
list1.Intersect(list2).Count() == list1.Count())
Console.WriteLine("Results are identical");
else
Console.WriteLine("Results differ");
foreach (XElement el in list1)
Console.WriteLine(el);
Dim cpo As XDocument = XDocument.Load("PurchaseOrders.xml")
Dim po As XElement = cpo.Root.<PurchaseOrder>.<Address>.FirstOrDefault
' LINQ to XML query
Dim list1 As IEnumerable(Of XElement) = po.Elements()
' XPath expression
Dim list2 As IEnumerable(Of XElement) = po.XPathSelectElements("./*")
If (list1.Count() = list2.Count()) AndAlso _
(list1.Intersect(list2).Count() = list1.Count()) Then
Console.WriteLine("Results are identical")
Else
Console.WriteLine("Results differ")
End If
For Each el As XElement In list1
Console.WriteLine(el)
Next
This example produces the following output:
Results are identical
<Name>Ellen Adams</Name>
<Street>123 Maple Street</Street>
<City>Mill Valley</City>
<State>CA</State>
<Zip>10999</Zip>
<Country>USA</Country>
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for