XPathEvaluate Method (XNode, String)

Extensions.XPathEvaluate Method (XNode, String)


Evaluates an XPath expression.

Namespace:   System.Xml.XPath
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

Public Shared Function XPathEvaluate (
	node As XNode,
	expression As String
) As Object


Type: System.Xml.Linq.XNode

The XNode on which to evaluate the XPath expression.

Type: System.String

A String that contains an XPath expression.

Return Value

Type: System.Object

An object that can contain a bool, a double, a string, or an IEnumerable(Of T).

If the collection is an enumeration of elements or attributes, you can use the Cast operator to get a collection of XElement or XAttribute.

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 with an attribute, then uses the XPathEvaluate method to retrieve the attribute.

Dim d As XDocument = _
    <?xml version='1.0'?>
    <root a='value'/>
Dim att As IEnumerable = CType(d.XPathEvaluate("/root/@a"), IEnumerable)
Console.WriteLine(att.Cast(Of XAttribute)().FirstOrDefault())

This example produces the following output:


Universal Windows Platform
Available since 10
.NET Framework
Available since 3.5
Available since 4.0
Return to top
© 2015 Microsoft