XPathSelectElements Method (XNode, String)
Collapse the table of content
Expand the table of content

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)

<ExtensionAttribute>
Public Shared Function XPathSelectElements (
	node As XNode,
	expression As String
) As IEnumerable(Of 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.

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.


                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>

.NET Framework
Available since 3.5
Silverlight
Available since 4.0
Return to top
Show:
© 2016 Microsoft