Extensions.XPathSelectElements 方法

定義

使用 XPath 表達式選擇一組元素。

多載

名稱 Description
XPathSelectElements(XNode, String)

使用 XPath 表達式選擇一組元素。

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

使用 XPath 表達式選取一組元素,並用指定的 IXmlNamespaceResolver命名空間前綴解析。

備註

雖然 XML XPath 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。

請注意,即使使用反向軸(如 preceding-siblingancestor-or-self),節點也會依文件順序回傳。

XPathSelectElements(XNode, String)

來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs

使用 XPath 表達式選擇一組元素。

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 表達式的資訊 XNode

expression
String

String A 包含 XPath 表達式。

傳回

其中 一個IEnumerable<T>XElement包含所選元素的元素。

範例

以下範例建立一個小型 XML 樹,並用 XPathSelectElements 來選擇一組元素。

                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

此範例會產生下列輸出:

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

備註

雖然 XML XPath 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。

請注意,即使使用反向軸(如 preceding-siblingancestor-or-self),節點也會依文件順序回傳。

適用於

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs
來源:
XNodeNavigator.cs

使用 XPath 表達式選取一組元素,並用指定的 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);
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 表達式的資訊 XNode

expression
String

String A 包含 XPath 表達式。

resolver
IXmlNamespaceResolver

A IXmlNamespaceResolver 代表 XPath 表達式中的命名空間前綴。

傳回

其中 一個IEnumerable<T>XElement包含所選元素的元素。

範例

此範例建立一棵包含命名空間的 XML 樹。 它使用 來 XmlReader 讀取 XML 文件。 接著它從 中得到一個XmlNameTableXmlReaderXmlNamespaceManager 中得到一個。XmlNameTable 它在選擇元素列表時會使用 。XmlNamespaceManager

                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

此範例會產生下列輸出:

<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 表達式。

雖然 XML XPath 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。

請注意,即使使用反向軸(如 preceding-siblingancestor-or-self),節點也會依文件順序回傳。

適用於