Extensions.XPathSelectElement Method (XNode, String)

 

Selects an XElement using a XPath expression.

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

public static XElement XPathSelectElement(
	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.Xml.Linq.XElement

An XElement, or null.

The following example creates a small XML tree and uses XPathSelectElement to select a single element.


                XElement root = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
    new XElement("Child6", 6)
);
XElement el = root.XPathSelectElement("./Child4");
Console.WriteLine(el);

This example produces the following output:

<Child4>4</Child4>

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