Export (0) Print
Expand All

Extensions.XPathEvaluate Method (XNode, String)

Evaluates an XPath expression.

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

public static Object XPathEvaluate(
	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.Object
An object that can contain a bool, a double, a string, or an IEnumerable<T>.

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).

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.

String xml = "<root a='value'/>";
XDocument d = XDocument.Parse(xml);
IEnumerable att = (IEnumerable)d.XPathEvaluate("/root/@a");
Console.WriteLine(att.Cast<XAttribute>().FirstOrDefault());

This example produces the following output:

a="value"

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Community Additions

ADD
Show:
© 2014 Microsoft