更新:2007 年 11 月
返回此节点的经过筛选的上级元素的集合。集合中只包括具有匹配 XName 的元素。
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 function Ancestors( name : XName ) : IEnumerable<XElement>
要匹配的 XName。
此节点上级元素的 XElement 的 IEnumerable<(Of <(T>)>)。集合中只包括具有匹配 XName 的元素。
返回的集合中的节点顺序与文档顺序相反。
此方法使用延迟执行。
此方法不在结果中返回自身。
下面的示例使用此方法。
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
该示例产生下面的输出:
Child
Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC