Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Extensions Class
 XPathSelectElements Method (XNode, ...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Extensions..::.XPathSelectElements Method (XNode, String)

Selects a collection of elements using an XPath expression.

Namespace:  System.Xml.XPath
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)
Visual Basic (Declaration)
<ExtensionAttribute> _
Public Shared Function XPathSelectElements ( _
    node As XNode, _
    expression As String _
) As IEnumerable(Of XElement)
Visual Basic (Usage)
Dim node As XNode
Dim expression As String
Dim returnValue As IEnumerable(Of XElement)

returnValue = node.XPathSelectElements(expression)
C#
public static IEnumerable<XElement> XPathSelectElements(
    this XNode node,
    string expression
)
Visual C++
[ExtensionAttribute]
public:
static IEnumerable<XElement^>^ XPathSelectElements(
    XNode^ node, 
    String^ expression
)
JScript
public static function XPathSelectElements(
    node : XNode, 
    expression : String
) : IEnumerable<XElement>

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<(Of <(XElement>)>)
An IEnumerable<(Of <(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).

Although the ordering of returned collections is not specified in the XML XPath Language 1.0 Recommendation, this extension method returns nodes in document order.

Note that nodes are returned in document order even when you use a reverse axis, such as preceding-sibling or ancestor-or-self.

The following example creates a small XML tree and uses XPathSelectElements to select a set of elements.

C#
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);
Visual Basic
Dim root As XElement = _
    <Root>
        <Child1>1</Child1>
        <Child1>2</Child1>
        <Child1>3</Child1>
        <Child2>4</Child2>
        <Child2>5</Child2>
        <Child2>6</Child2>
    </Root>
Dim list As IEnumerable(Of XElement) = root.XPathSelectElements("./Child2")
For Each el As XElement In list
    Console.WriteLine(el)
Next

This example produces the following output:

<Child2>4</Child2>
<Child2>5</Child2>
<Child2>6</Child2>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker