语言

Extensions.XPathEvaluate 方法

定义

计算 XPath 表达式。

重载

名称 说明
XPathEvaluate(XNode, String)

计算 XPath 表达式。

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

计算 XPath 表达式,并使用指定的 IXmlNamespaceResolver命名空间前缀解析命名空间前缀。

注解

尽管 XML XPath 语言 1.0 建议中未指定返回集合的排序,但此扩展方法按文档顺序返回节点。

请注意,即使在使用反向轴(如 preceding-sibling 或 ancestor-or-self)时,也会按文档顺序返回节点。

XPathEvaluate(XNode, String)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
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

要 XNode 计算 XPath 表达式的依据。

expression
String

包含 XPath 表达式的 A String 。

返回

一个对象,可以包含一个bool、一个doublestring或一个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运算符获取或XElementXAttribute集合。

尽管 XML XPath 语言 1.0 建议中未指定返回集合的排序,但此扩展方法按文档顺序返回节点。

请注意,即使在使用反向轴(如 preceding-sibling 或 ancestor-or-self)时,也会按文档顺序返回节点。

适用于

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

Source:
XNodeNavigator.cs
Source:
XNodeNavigator.cs
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

要 XNode 计算 XPath 表达式的依据。

expression
String

包含 XPath 表达式的 A String 。

resolver
IXmlNamespaceResolver

XPath 表达式中命名空间前缀的 A IXmlNamespaceResolver 。

返回

一个包含计算表达式结果的对象。 对象可以是一个 bool、一个 double、一个 string或一个 IEnumerable<T>。

示例

以下示例创建一个包含命名空间的 XML 树。 它使用一个 XmlReader 读取 XML 文档。 然后,它从XmlNameTable中获取一个XmlReader和一个 。XmlNamespaceManagerXmlNameTable 它在选择元素时使用 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)时,也会按文档顺序返回节点。

适用于