Extensions.XPathSelectElements Method (XNode, String)
Selects a collection of elements using an XPath expression.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
public static IEnumerable<XElement> XPathSelectElements( this XNode node, string expression )
Parameters
- node
- Type: System.Xml.Linq.XNode
The XNode on which to evaluate the XPath expression.
- expression
- Type: System.String
A String that contains an XPath expression.
Return Value
Type: System.Collections.Generic.IEnumerable<XElement>An IEnumerable<T> of XElement that contains the selected elements.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type XNode. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).The following example creates a small XML tree and uses XPathSelectElements to select a set of elements.
XElement root = new XElement("Root", new XElement("Child1", 1), new XElement("Child1", 2), new XElement("Child1", 3), new XElement("Child2", 4), new XElement("Child2", 5), new XElement("Child2", 6) ); IEnumerable<XElement> list = root.XPathSelectElements("./Child2"); foreach (XElement el in list) Console.WriteLine(el);
This example produces the following output:
<Child2>4</Child2> <Child2>5</Child2> <Child2>6</Child2>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.