Extensions.XPathSelectElements 方法

定義

使用 XPath 運算式選取項目的集合。

多載

XPathSelectElements(XNode, String)

使用 XPath 運算式選取項目的集合。

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

使用 XPath 運算式選取項目集合,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。

備註

雖然 XML XPath Language 1.0 建議中未指定傳回集合的順序,但此擴充方法會依檔順序傳回節點。

請注意,即使您使用反向軸,例如 preceding-siblingancestor-or-self ,節點仍會以檔順序傳回。

XPathSelectElements(XNode, String)

使用 XPath 運算式選取項目的集合。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements (this System.Xml.Linq.XNode node, string expression);

參數

node
XNode

要針對其評估 XPath 運算式的 XNode

expression
String

包含 XPath 運算式的 String

傳回

IEnumerable<XElement>

包含已選取項目之 IEnumerable<T>XElement

範例

下列範例會建立小型 XML 樹狀結構,並使用 XPathSelectElements 來選取一組項目。

C#
                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);  

這個範例會產生下列輸出:

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

備註

雖然 XML XPath Language 1.0 建議中未指定傳回集合的順序,但此擴充方法會依檔順序傳回節點。

請注意,即使您使用反向軸,例如 preceding-siblingancestor-or-self ,節點仍會以檔順序傳回。

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 2.0, 2.1

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

使用 XPath 運算式選取項目集合,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);

參數

node
XNode

要針對其評估 XPath 運算式的 XNode

expression
String

包含 XPath 運算式的 String

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver,用來解析 XPath 運算式中的命名空間後置字元。

傳回

IEnumerable<XElement>

包含已選取項目之 IEnumerable<T>XElement

範例

此範例會建立包含命名空間的 XML 樹狀結構。 該範例會使用 XmlReader 來讀取 XML 文件。 接著,它會從 XmlNameTable 取得 XmlReader,並從 XmlNamespaceManager 取得 XmlNameTable。 它會在選取專案清單時使用 XmlNamespaceManager

C#
                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);  

這個範例會產生下列輸出:

XML
<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 Language 1.0 建議中未指定傳回集合的順序,但此擴充方法會依檔順序傳回節點。

請注意,即使您使用反向軸,例如 preceding-siblingancestor-or-self ,節點仍會以檔順序傳回。

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 2.0, 2.1