How to: Find Elements in a Namespace (XPath-LINQ to XML)
XPath expressions can find nodes in a particular namespace. XPath expressions use namespace prefixes for specifying namespaces. To parse an XPath expression that contains namespace prefixes, you must pass an object to the XPath methods that implements IXmlNamespaceResolver. This example uses XmlNamespaceManager.
The XPath expression is:
./aw:*
Example
The following example reads an XML tree that contains two namespaces. It uses an XmlReader to read the XML document. It then gets an XmlNameTable from the XmlReader, and an XmlNamespaceManager from the XmlNameTable. It uses the XmlNamespaceManager when selecting elements.
XmlReader reader = XmlReader.Create("ConsolidatedPurchaseOrders.xml");
XElement root = XElement.Load(reader);
XmlNameTable nameTable = reader.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("aw", "https://www.adventure-works.com");
IEnumerable<XElement> list1 = root.XPathSelectElements("./aw:*", namespaceManager);
IEnumerable<XElement> list2 =
from el in root.Elements()
where el.Name.Namespace == "https://www.adventure-works.com"
select el;
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 list2)
Console.WriteLine(el);
Dim reader As XmlReader = _
XmlReader.Create("ConsolidatedPurchaseOrders.xml")
Dim root As XElement = XElement.Load(reader)
Dim nameTable As XmlNameTable = reader.NameTable
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)
namespaceManager.AddNamespace("aw", "https://www.adventure-works.com")
Dim list1 As IEnumerable(Of XElement) = _
root.XPathSelectElements("./aw:*", namespaceManager)
Dim list2 As IEnumerable(Of XElement) = _
From el In root.Elements() _
Where el.Name.Namespace = "https://www.adventure-works.com" _
Select el
If list1.Count() = list2.Count() And _
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 list2
Console.WriteLine(el)
Next
This example produces the following output:
Results are identical
<aw:PurchaseOrder PONumber="11223" Date="2000-01-15" xmlns:aw="https://www.adventure-works.com">
<aw:ShippingAddress>
<aw:Name>Chris Preston</aw:Name>
<aw:Street>123 Main St.</aw:Street>
<aw:City>Seattle</aw:City>
<aw:State>WA</aw:State>
<aw:Zip>98113</aw:Zip>
<aw:Country>USA</aw:Country>
</aw:ShippingAddress>
<aw:BillingAddress>
<aw:Name>Chris Preston</aw:Name>
<aw:Street>123 Main St.</aw:Street>
<aw:City>Seattle</aw:City>
<aw:State>WA</aw:State>
<aw:Zip>98113</aw:Zip>
<aw:Country>USA</aw:Country>
</aw:BillingAddress>
<aw:DeliveryInstructions>Ship only complete order.</aw:DeliveryInstructions>
<aw:Item PartNum="LIT-01">
<aw:ProductID>Litware Networking Card</aw:ProductID>
<aw:Qty>1</aw:Qty>
<aw:Price>20.99</aw:Price>
</aw:Item>
<aw:Item PartNum="LIT-25">
<aw:ProductID>Litware 17in LCD Monitor</aw:ProductID>
<aw:Qty>1</aw:Qty>
<aw:Price>199.99</aw:Price>
</aw:Item>
</aw:PurchaseOrder>