Extensions.XPathEvaluate 方法

定義

評估 XPath 運算式。

多載

XPathEvaluate(XNode, String)

評估 XPath 運算式。

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

評估 XPath 運算式,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。

備註

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

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

XPathEvaluate(XNode, String)

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

評估 XPath 運算式。

C#
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression);

參數

node
XNode

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

expression
String

包含 XPath 運算式的 String

傳回

物件,可以包含 booldoublestringIEnumerable<T>

範例

下列範例會建立具有 屬性的小型 XML 樹狀結構,然後使用 XPathEvaluate 方法來擷取 屬性。

C#
                String xml = "<root a='value'/>";  
XDocument d = XDocument.Parse(xml);  
IEnumerable att = (IEnumerable)d.XPathEvaluate("/root/@a");  
Console.WriteLine(att.Cast<XAttribute>().FirstOrDefault());  

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

a="value"  

備註

如果集合是專案或屬性的列舉,您可以使用 Cast 運算子來取得 或 XAttributeXElement 集合。

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

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

適用於

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

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

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

評估 XPath 運算式,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。

C#
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
C#
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);

參數

node
XNode

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

expression
String

包含 XPath 運算式的 String

resolver
IXmlNamespaceResolver

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

傳回

物件,包含評估運算式的結果。 這個物件可以是 booldoublestringIEnumerable<T>

範例

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

C#
                string markup =  
@"<aw:Root xmlns:aw='http://www.adventure-works.com'>  
    <aw:Child1 aw:Att='attdata'>child one data 1</aw:Child1>  
</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 atts = (IEnumerable)root.XPathEvaluate("./aw:Child1/@aw:Att", namespaceManager);  
IEnumerable<XAttribute> attList = atts.Cast<XAttribute>();  
XAttribute att = attList.First();  
Console.WriteLine(att);  

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

aw:Att="attdata"  

備註

您可以使用這個方法來評估包含命名空間前置詞的 XPath 運算式。

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

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

適用於

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