XNode.Ancestors Method (XName)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns a filtered collection of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
Parameters
- name
- Type: System.Xml.Linq.XName
The XName to match.
Return Value
Type: System.Collections.Generic.IEnumerable(Of XElement)An IEnumerable(Of T) of XElement of the ancestor elements of this node. Only elements that have a matching XName are included in the collection.
The nodes in the returned collection are in reverse document order.
This method uses deferred execution.
The following example uses this method.
StringBuilder output = new StringBuilder(); 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")) output.Append(el.Name + Environment.NewLine); OutputTextBlock.Text = output.ToString();
Show: