Extensions.XPathSelectElements 方法

定义

使用 XPath 表达式选择元素集合。

重载

名称 说明
XPathSelectElements(XNode, String)

使用 XPath 表达式选择元素集合。

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

使用 XPath 表达式选择元素集合,并使用指定的 IXmlNamespaceResolver命名空间前缀解析命名空间前缀。

注解

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

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

XPathSelectElements(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::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ XPathSelectElements(System::Xml::Linq::XNode ^ node, System::String ^ expression);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements(this System.Xml.Linq.XNode node, string expression);
static member XPathSelectElements : System.Xml.Linq.XNode * string -> seq<System.Xml.Linq.XElement>
<Extension()>
Public Function XPathSelectElements (node As XNode, expression As String) As IEnumerable(Of XElement)

参数

node
XNode

XNode 计算 XPath 表达式的依据。

expression
String

包含 XPath 表达式的 A String

返回

IEnumerable<T>其中一个XElement包含所选元素。

示例

以下示例创建一个小 XML 树,并用于 XPathSelectElements 选择一组元素。

                XElement root = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child1", 2),
    new XElement("Child1", 3),
    new XElement("Child2", 4),
    new XElement("Child2", 5),
    new XElement("Child2", 6)
);
IEnumerable<XElement> list = root.XPathSelectElements("./Child2");
foreach (XElement el in list)
    Console.WriteLine(el);
                Dim root As XElement = _
    <Root>
        <Child1>1</Child1>
        <Child1>2</Child1>
        <Child1>3</Child1>
        <Child2>4</Child2>
        <Child2>5</Child2>
        <Child2>6</Child2>
    </Root>
Dim list As IEnumerable(Of XElement) = root.XPathSelectElements("./Child2")
For Each el As XElement In list
    Console.WriteLine(el)
Next

此示例生成以下输出:

<Child2>4</Child2>
<Child2>5</Child2>
<Child2>6</Child2>

注解

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

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

适用于

XPathSelectElements(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::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ XPathSelectElements(System::Xml::Linq::XNode ^ node, System::String ^ expression, System::Xml::IXmlNamespaceResolver ^ resolver);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements(this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> XPathSelectElements(this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);
static member XPathSelectElements : System.Xml.Linq.XNode * string * System.Xml.IXmlNamespaceResolver -> seq<System.Xml.Linq.XElement>
<Extension()>
Public Function XPathSelectElements (node As XNode, expression As String, resolver As IXmlNamespaceResolver) As IEnumerable(Of XElement)

参数

node
XNode

XNode 计算 XPath 表达式的依据。

expression
String

包含 XPath 表达式的 A String

resolver
IXmlNamespaceResolver

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

返回

IEnumerable<T>其中一个XElement包含所选元素。

示例

此示例创建一个包含命名空间的 XML 树。 它使用一个 XmlReader 读取 XML 文档。 然后,它从XmlNameTable中获取一个XmlReader和一个 。XmlNamespaceManagerXmlNameTable 它在选择元素列表时使用 XmlNamespaceManager

                string markup = @"
<aw:Root xmlns:aw='http://www.adventure-works.com'>
    <aw:Child1>child one data 1</aw:Child1>
    <aw:Child1>child one data 2</aw:Child1>
    <aw:Child1>child one data 3</aw:Child1>
    <aw:Child2>child two data 4</aw:Child2>
    <aw:Child2>child two data 5</aw:Child2>
    <aw:Child2>child two data 6</aw:Child2>
</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<XElement> elements = root.XPathSelectElements("./aw:Child1", namespaceManager);
foreach (XElement el in elements)
    Console.WriteLine(el);
                Dim markup As XElement = _
<aw:Root xmlns:aw="http://www.adventure-works.com">
    <aw:Child1>child one data 1</aw:Child1>
    <aw:Child1>child one data 2</aw:Child1>
    <aw:Child1>child one data 3</aw:Child1>
    <aw:Child2>child two data 4</aw:Child2>
    <aw:Child2>child two data 5</aw:Child2>
    <aw:Child2>child two data 6</aw:Child2>
</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 elements As IEnumerable(Of XElement) = markup.XPathSelectElements("./aw:Child1", namespaceManager)
For Each el As XElement In elements
    Console.WriteLine(el)
Next

此示例生成以下输出:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">child one data 1</aw:Child1>
<aw:Child1 xmlns:aw="http://www.adventure-works.com">child one data 2</aw:Child1>
<aw:Child1 xmlns:aw="http://www.adventure-works.com">child one data 3</aw:Child1>

注解

可以使用此方法计算包含命名空间前缀的 XPath 表达式。

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

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

适用于