Extensions.XPathSelectElement Method

Definition

Selects an XElement using a XPath expression.

Overloads

XPathSelectElement(XNode, String)

Selects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs

Selects an XElement using a XPath expression.

C#
public static System.Xml.Linq.XElement? XPathSelectElement(this System.Xml.Linq.XNode node, string expression);
C#
public static System.Xml.Linq.XElement XPathSelectElement(this System.Xml.Linq.XNode node, string expression);

Parameters

node
XNode

The XNode on which to evaluate the XPath expression.

expression
String

A String that contains an XPath expression.

Returns

An XElement, or null.

Examples

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

C#
                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:

XML
<Child4>4</Child4>  

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs

Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

C#
public static System.Xml.Linq.XElement? XPathSelectElement(this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
C#
public static System.Xml.Linq.XElement XPathSelectElement(this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);

Parameters

node
XNode

The XNode on which to evaluate the XPath expression.

expression
String

A String that contains an XPath expression.

resolver
IXmlNamespaceResolver

An IXmlNamespaceResolver for the namespace prefixes in the XPath expression.

Returns

An XElement, or null.

Examples

The following example creates an XML tree that contains a namespace. It uses an XmlReader to read the XML document. It then gets an XmlNameTable from the XmlReader, and an XmlNamespaceManager from the XmlNameTable. It uses the XmlNamespaceManager when selecting an element.

C#
                string markup = @"  
<aw:Root xmlns:aw='http://www.adventure-works.com'>  
    <aw:Child1>child one data</aw:Child1>  
    <aw:Child2>child two data</aw:Child2>  
</aw:Root>";  
XmlReader reader = XmlReader.Create(new StringReader(markup));  
XElement root = XElement.Load(reader);  
XmlNameTable nameTable = reader.NameTable;  
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);  
namespaceManager.AddNamespace("aw", "http://www.adventure-works.com");  
XElement child1 = root.XPathSelectElement("./aw:Child1", namespaceManager);  
Console.WriteLine(child1);  

This example produces the following output:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">child one data</aw:Child1>  

Remarks

You can use this method to evaluate XPath expressions that contain namespace prefixes.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1