Breyta

Deila með


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.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Linq::XElement ^ XPathSelectElement(System::Xml::Linq::XNode ^ node, System::String ^ expression);
public static System.Xml.Linq.XElement? XPathSelectElement (this System.Xml.Linq.XNode node, string expression);
public static System.Xml.Linq.XElement XPathSelectElement (this System.Xml.Linq.XNode node, string expression);
static member XPathSelectElement : System.Xml.Linq.XNode * string -> System.Xml.Linq.XElement
<Extension()>
Public Function XPathSelectElement (node As XNode, expression As String) As XElement

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.

                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);  
                Dim root As XElement = _  
    <Root>  
        <Child1>1</Child1>  
        <Child2>2</Child2>  
        <Child3>3</Child3>  
        <Child4>4</Child4>  
        <Child5>5</Child5>  
        <Child6>6</Child6>  
    </Root>  
Dim el As XElement = root.XPathSelectElement("./Child4")  
Console.WriteLine(el)  

This example produces the following output:

<Child4>4</Child4>  

Applies to

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.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Linq::XElement ^ XPathSelectElement(System::Xml::Linq::XNode ^ node, System::String ^ expression, System::Xml::IXmlNamespaceResolver ^ resolver);
public static System.Xml.Linq.XElement? XPathSelectElement (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
public static System.Xml.Linq.XElement XPathSelectElement (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);
static member XPathSelectElement : System.Xml.Linq.XNode * string * System.Xml.IXmlNamespaceResolver -> System.Xml.Linq.XElement
<Extension()>
Public Function XPathSelectElement (node As XNode, expression As String, resolver As IXmlNamespaceResolver) As XElement

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.

                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);  
                Dim markup As XElement = _  
    <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>  
Dim reader As XmlReader = markup.CreateReader  
Dim nameTable As XmlNameTable = reader.NameTable  
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)  
namespaceManager.AddNamespace("aw", "http://www.adventure-works.com")  
Dim child1 As XElement = markup.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