.NET Framework 类库
XNode..::.Ancestors 方法 (XName)

更新:2007 年 11 月

返回此节点的经过筛选的上级元素的集合。集合中只包括具有匹配 XName 的元素。

命名空间:  System.Xml.Linq
程序集:  System.Xml.Linq(在 System.Xml.Linq.dll 中)

语法

Visual Basic(声明)
Public Function Ancestors ( _
    name As XName _
) As IEnumerable(Of XElement)
Visual Basic (用法)
Dim instance As XNode
Dim name As XName
Dim returnValue As IEnumerable(Of XElement)

returnValue = instance.Ancestors(name)
C#
public IEnumerable<XElement> Ancestors(
    XName name
)
Visual C++
public:
IEnumerable<XElement^>^ Ancestors(
    XName^ name
)
J#
public IEnumerable<XElement> Ancestors(
    XName name
)
JScript
public function Ancestors(
    name : XName
) : IEnumerable<XElement>

参数

name
类型:System.Xml.Linq..::.XName

要匹配的 XName

返回值

类型:System.Collections.Generic..::.IEnumerable<(Of <(XElement>)>)

此节点上级元素的 XElementIEnumerable<(Of <(T>)>)。集合中只包括具有匹配 XName 的元素。

返回的集合中的节点顺序与文档顺序相反。

此方法使用延迟执行。

备注

此方法不在结果中返回自身。

示例

下面的示例使用此方法。

C#
XElement xmlTree = new XElement("Root",
    new XElement("Child", 
        new XElement("GrandChild", "content")
    )
);
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");
foreach (XElement el in grandChild.Ancestors("Child"))
    Console.WriteLine(el.Name);
Visual Basic
Dim xmlTree As XElement = _ 
        <Root>
            <Child>
                <GrandChild>content</GrandChild>
            </Child>
        </Root>

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>
For Each el In grandChild.Ancestors("Child")
    Console.WriteLine(el.Name)
Next

该示例产生下面的输出:

Child
平台

Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5

.NET Compact Framework

受以下版本支持:3.5
另请参见

参考

其他资源

标记 :


Page view tracker