更新:2007 年 11 月
返回此节点的经过筛选的上级元素的集合。集合中只包括具有匹配 XName 的元素。
命名空间:
System.Xml.Linq 程序集:
System.Xml.Linq(在 System.Xml.Linq.dll 中)
Public Function Ancestors ( _
name As XName _
) As IEnumerable(Of XElement)
Dim instance As XNode
Dim name As XName
Dim returnValue As IEnumerable(Of XElement)
returnValue = instance.Ancestors(name)
public IEnumerable<XElement> Ancestors(
XName name
)
public:
IEnumerable<XElement^>^ Ancestors(
XName^ name
)
public IEnumerable<XElement> Ancestors(
XName name
)
public function Ancestors(
name : XName
) : IEnumerable<XElement>
此方法不在结果中返回自身。
下面的示例使用此方法。
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);
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
该示例产生下面的输出:
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
参考
其他资源