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)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs

評估 XPath 運算式。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Object ^ XPathEvaluate(System::Xml::Linq::XNode ^ node, System::String ^ expression);
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression);
static member XPathEvaluate : System.Xml.Linq.XNode * string -> obj
<Extension()>
Public Function XPathEvaluate (node As XNode, expression As String) As Object

參數

node
XNode

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

expression
String

包含 XPath 運算式的 String

傳回

物件,可以包含 booldoublestringIEnumerable<T>

範例

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

                String xml = "<root a='value'/>";  
XDocument d = XDocument.Parse(xml);  
IEnumerable att = (IEnumerable)d.XPathEvaluate("/root/@a");  
Console.WriteLine(att.Cast<XAttribute>().FirstOrDefault());  
                Dim d As XDocument = _  
    <?xml version='1.0'?>  
    <root a='value'/>  
Dim att As IEnumerable = CType(d.XPathEvaluate("/root/@a"), IEnumerable)  
Console.WriteLine(att.Cast(Of XAttribute)().FirstOrDefault())  

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

a="value"  

備註

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

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

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

適用於

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs

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

public:
[System::Runtime::CompilerServices::Extension]
 static System::Object ^ XPathEvaluate(System::Xml::Linq::XNode ^ node, System::String ^ expression, System::Xml::IXmlNamespaceResolver ^ resolver);
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
public static object XPathEvaluate (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);
static member XPathEvaluate : System.Xml.Linq.XNode * string * System.Xml.IXmlNamespaceResolver -> obj
<Extension()>
Public Function XPathEvaluate (node As XNode, expression As String, resolver As IXmlNamespaceResolver) As Object

參數

node
XNode

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

expression
String

包含 XPath 運算式的 String

resolver
IXmlNamespaceResolver

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

傳回

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

範例

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

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

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

aw:Att="attdata"  

備註

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

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

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

適用於