Extensions.XPathEvaluate 方法

定義

評估 XPath 運算式。Evaluates an XPath expression.

多載

XPathEvaluate(XNode, String)

評估 XPath 運算式。Evaluates an XPath expression.

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

評估 XPath 運算式,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。Evaluates 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.

XPathEvaluate(XNode, String)

評估 XPath 運算式。Evaluates an XPath expression.

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 運算式的 XNodeThe XNode on which to evaluate the XPath expression.

expression
String

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

傳回

Object

物件,可以包含 booldoublestringIEnumerable<T>An object that can contain a bool, a double, a string, or an IEnumerable<T>.

範例

下列範例會建立具有屬性的小型 XML 樹狀結構,然後使用 XPathEvaluate 方法來取出屬性。The following example creates a small XML tree with an attribute, then uses the XPathEvaluate method to retrieve the attribute.

  
                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())  

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

a="value"  

備註

如果集合是元素或屬性的列舉,您可以使用 Cast 運算子來取得 XElementXAttribute的集合。If the collection is an enumeration of elements or attributes, you can use the Cast operator to get a collection of XElement or XAttribute.

雖然未在 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.

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

評估 XPath 運算式,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。Evaluates an XPath expression, resolving namespace prefixes using the specified 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);
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 運算式的 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.

傳回

Object

物件,包含評估運算式的結果。An object that contains the result of evaluating the expression. 這個物件可以是 booldoublestringIEnumerable<T>The object can be a bool, a double, a string, or an IEnumerable<T>.

範例

下列範例會建立包含命名空間的 XML 樹狀結構。The following 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 an element.

  
                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)  

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

aw:Att="attdata"  

備註

您可以使用這個方法來評估包含命名空間前置詞的 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.

適用於