title | description | ms.date | dev_langs | ms.topic | ||
---|---|---|---|---|---|---|
How to filter on element names - LINQ to XML |
Learn how to filter on the element name when you call a method that returns an IEnumerable of XElement. |
07/20/2015 |
|
how-to |
When you call one of the methods that return xref:System.Collections.Generic.IEnumerable%601 of xref:System.Xml.Linq.XElement, you can filter on the element name.
This example retrieves a collection of descendants that's filtered to contain only descendants with the specified name.
This example uses XML document Sample XML file: Typical purchase order.
XElement po = XElement.Load("PurchaseOrder.xml");
IEnumerable<XElement> items =
from el in po.Descendants("ProductName")
select el;
foreach(XElement prdName in items)
Console.WriteLine(prdName.Name + ":" + (string) prdName);
Dim po As XElement = XElement.Load("PurchaseOrder.xml")
Dim items As IEnumerable(Of XElement) = _
From el In po...<ProductName> _
Select el
For Each prdName As XElement In items
Console.WriteLine(prdName.Name.ToString & ":" & prdName.Value)
Next
This example produces the following output:
ProductName:Lawnmower
ProductName:Baby Monitor
The other methods that return xref:System.Collections.Generic.IEnumerable%601 of xref:System.Xml.Linq.XElement collections follow the same pattern. Their signatures are similar to xref:System.Xml.Linq.XContainer.Elements%2A and xref:System.Xml.Linq.XContainer.Descendants%2A. The following is the complete list of methods that have similar method signatures:
- xref:System.Xml.Linq.XNode.Ancestors%2A
- xref:System.Xml.Linq.XContainer.Descendants%2A
- xref:System.Xml.Linq.XContainer.Elements%2A
- xref:System.Xml.Linq.XNode.ElementsAfterSelf%2A
- xref:System.Xml.Linq.XNode.ElementsBeforeSelf%2A
- xref:System.Xml.Linq.XElement.AncestorsAndSelf%2A
- xref:System.Xml.Linq.XElement.DescendantsAndSelf%2A
The following example shows the same query for XML that's in a namespace. For more information, see Namespaces overview.
The example uses XML document Sample XML file: Typical purchase order in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement po = XElement.Load("PurchaseOrderInNamespace.xml");
IEnumerable<XElement> items =
from el in po.Descendants(aw + "ProductName")
select el;
foreach (XElement prdName in items)
Console.WriteLine(prdName.Name + ":" + (string)prdName);
Imports <xmlns:aw="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim po As XElement = XElement.Load("PurchaseOrderInNamespace.xml")
Dim items As IEnumerable(Of XElement) = _
From el In po...<aw:ProductName> _
Select el
For Each prdName As XElement In items
Console.WriteLine(prdName.Name.ToString & ":" & prdName.Value)
Next
End Sub
End Module
This example produces the following output:
{http://www.adventure-works.com}ProductName:Lawnmower
{http://www.adventure-works.com}ProductName:Baby Monitor