Extensions.XPathEvaluate 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
評估一個 XPath 表達式。
多載
| 名稱 | Description |
|---|---|
| XPathEvaluate(XNode, String) |
評估一個 XPath 表達式。 |
| XPathEvaluate(XNode, String, IXmlNamespaceResolver) |
評估 XPath 表達式,並利用指定的 IXmlNamespaceResolver。 |
備註
雖然 XML XPath 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。
請注意,即使使用反向軸(如 preceding-sibling 或 ancestor-or-self),節點也會依文件順序回傳。
XPathEvaluate(XNode, String)
評估一個 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
參數
傳回
一個可以包含 bool、a double、 stringa 、 或 IEnumerable<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子得到 或 XElement的集合XAttribute。
雖然 XML XPath 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。
請注意,即使使用反向軸(如 preceding-sibling 或 ancestor-or-self),節點也會依文件順序回傳。
適用於
XPathEvaluate(XNode, String, IXmlNamespaceResolver)
評估 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
參數
- resolver
- IXmlNamespaceResolver
A IXmlNamespaceResolver 代表 XPath 表達式中的命名空間前綴。
傳回
一個包含求值表達式結果的物件。 物件可以是 bool、 、 double、 string或 IEnumerable<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 語言 1.0 建議中未規定回傳集合的順序,但此擴充方法會依文件順序回傳節點。
請注意,即使使用反向軸(如 preceding-sibling 或 ancestor-or-self),節點也會依文件順序回傳。