Extensions.XPathSelectElement 方法

定義

使用 XPath 運算式選取 XElementSelects an XElement using a XPath expression.

多載

XPathSelectElement(XNode, String)

使用 XPath 運算式選取 XElementSelects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

使用 XPath 運算式選取 XElement,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String)

使用 XPath 運算式選取 XElementSelects 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

參數

node
XNode

要針對其評估 XPath 運算式的 XNodeThe XNode on which to evaluate the XPath expression.

expression
String

包含 XPath 運算式的 StringA String that contains an XPath expression.

傳回

XElement

XElement 或 null。An XElement, or null.

範例

下列範例會建立一個小型 XML 樹狀結構,並使用 XPathSelectElement 來選取單一元素。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>  

適用於

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

使用 XPath 運算式選取 XElement,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。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

參數

node
XNode

要針對其評估 XPath 運算式的 XNodeThe XNode on which to evaluate the XPath expression.

expression
String

包含 XPath 運算式的 StringA String that contains an XPath expression.

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver,用來解析 XPath 運算式中的命名空間後置字元。An IXmlNamespaceResolver for the namespace prefixes in the XPath expression.

傳回

XElement

XElement 或 null。An XElement, or null.

範例

下列範例會建立包含命名空間的 XML 樹狀結構。The following example creates an XML tree that contains a namespace. 該範例會使用 XmlReader 來讀取 XML 文件。It uses an XmlReader to read the XML document. 接著,它會從 XmlNameTable 取得 XmlReader,並從 XmlNamespaceManager 取得 XmlNameTableIt then gets an XmlNameTable from the XmlReader, and an XmlNamespaceManager from the XmlNameTable. 它會在 XmlNamespaceManager 選取專案時使用。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>  

備註

您可以使用這個方法來評估包含命名空間前置詞的 XPath 運算式。You can use this method to evaluate XPath expressions that contain namespace prefixes.

適用於