Extensions.XPathSelectElements 方法

定義

使用 XPath 運算式選取項目的集合。Selects a collection of elements using an XPath expression.

多載

XPathSelectElements(XNode, String)

使用 XPath 運算式選取項目的集合。Selects a collection of elements using an XPath expression.

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

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

備註

雖然未在 XML XPath 語言1.0 建議中指定傳回集合的順序,但此擴充方法會以檔順序傳回節點。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.

請注意,即使您使用反向軸(例如 preceding-siblingancestor-or-self),仍會以檔順序傳回節點。Note that nodes are returned in document order even when you use a reverse axis, such as preceding-sibling or ancestor-or-self.

XPathSelectElements(XNode, String)

使用 XPath 運算式選取項目的集合。Selects a collection of elements using an XPath expression.

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

參數

node
XNode

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

expression
String

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

傳回

包含已選取項目之 IEnumerable<T>XElementAn IEnumerable<T> of XElement that contains the selected elements.

範例

下列範例會建立小型 XML 樹狀結構,並使用 XPathSelectElements 來選取一組項目。The following example creates a small XML tree and uses XPathSelectElements to select a set of elements.

  
                XElement root = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child1", 2),  
    new XElement("Child1", 3),  
    new XElement("Child2", 4),  
    new XElement("Child2", 5),  
    new XElement("Child2", 6)  
);  
IEnumerable<XElement> list = root.XPathSelectElements("./Child2");  
foreach (XElement el in list)  
    Console.WriteLine(el);  
  
                Dim root As XElement = _  
    <Root>  
        <Child1>1</Child1>  
        <Child1>2</Child1>  
        <Child1>3</Child1>  
        <Child2>4</Child2>  
        <Child2>5</Child2>  
        <Child2>6</Child2>  
    </Root>  
Dim list As IEnumerable(Of XElement) = root.XPathSelectElements("./Child2")  
For Each el As XElement In list  
    Console.WriteLine(el)  
Next  

這個範例會產生下列輸出:This example produces the following output:

<Child2>4</Child2>  
<Child2>5</Child2>  
<Child2>6</Child2>  

備註

雖然未在 XML XPath 語言1.0 建議中指定傳回集合的順序,但此擴充方法會以檔順序傳回節點。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.

請注意,即使您使用反向軸(例如 preceding-siblingancestor-or-self),仍會以檔順序傳回節點。Note that nodes are returned in document order even when you use a reverse axis, such as preceding-sibling or ancestor-or-self.

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

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

public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ XPathSelectElements(System::Xml::Linq::XNode ^ node, System::String ^ expression, System::Xml::IXmlNamespaceResolver ^ resolver);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);
static member XPathSelectElements : System.Xml.Linq.XNode * string * System.Xml.IXmlNamespaceResolver -> seq<System.Xml.Linq.XElement>
<Extension()>
Public Function XPathSelectElements (node As XNode, expression As String, resolver As IXmlNamespaceResolver) As IEnumerable(Of 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 運算式中的命名空間後置字元。A IXmlNamespaceResolver for the namespace prefixes in the XPath expression.

傳回

包含已選取項目之 IEnumerable<T>XElementAn IEnumerable<T> of XElement that contains the selected elements.

範例

這個範例會建立包含命名空間的 XML 樹狀結構。This 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. 選取專案清單時,它會使用 XmlNamespaceManagerIt uses the XmlNamespaceManager when selecting the list of elements.

  
                string markup = @"  
<aw:Root xmlns:aw='http://www.adventure-works.com'>  
    <aw:Child1>child one data 1</aw:Child1>  
    <aw:Child1>child one data 2</aw:Child1>  
    <aw:Child1>child one data 3</aw:Child1>  
    <aw:Child2>child two data 4</aw:Child2>  
    <aw:Child2>child two data 5</aw:Child2>  
    <aw:Child2>child two data 6</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");  
IEnumerable<XElement> elements = root.XPathSelectElements("./aw:Child1", namespaceManager);  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
  
                Dim markup As XElement = _  
<aw:Root xmlns:aw="http://www.adventure-works.com">  
    <aw:Child1>child one data 1</aw:Child1>  
    <aw:Child1>child one data 2</aw:Child1>  
    <aw:Child1>child one data 3</aw:Child1>  
    <aw:Child2>child two data 4</aw:Child2>  
    <aw:Child2>child two data 5</aw:Child2>  
    <aw:Child2>child two data 6</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 elements As IEnumerable(Of XElement) = markup.XPathSelectElements("./aw:Child1", namespaceManager)  
For Each el As XElement In elements  
    Console.WriteLine(el)  
Next  

這個範例會產生下列輸出:This example produces the following output:

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

備註

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

雖然未在 XML XPath 語言1.0 建議中指定傳回集合的順序,但此擴充方法會以檔順序傳回節點。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.

請注意,即使您使用反向軸(例如 preceding-siblingancestor-or-self),仍會以檔順序傳回節點。Note that nodes are returned in document order even when you use a reverse axis, such as preceding-sibling or ancestor-or-self.

適用於