How to get great parent and great parent xml node
any node above the current node in the example hierarchical tree the parent, grandfather, great-grandmother or rot node are the ancestors of the anode in xml
How can I get the great parent and great parent of the current node xml.
I am using VB.Net in ASP.NET application.
Thanks wow the answers are beautiful
Maybe I have something like XmlNode greatGrandParent = myNode.ParentNode.ParentNode.ParentNode.lastchild.lastchild
a source to share
Using System.Xml the XmlNode has a ParentNode property so you can use it to navigate the tree.
Using System.Xml.XPath XPathNavigator has a MoveToParent method.
Using System.Xml.Linq the XNode class has an Ancestors method which can be a little neat: -
GrandParent = Node.Ancestors.Skip(1).Single()
GreatGrandParent = Node.Ancestors.Skip(2).Single()
a source to share
In the (dim) light of the very limited information provided, I would assume that you are using the ParentNode property of the context object XmlNode
.
eg. Get Grandparents:
XmlNode grandParent = myNode.ParentNode.ParentNode;
eg. Get Grandparents:
XmlNode greatGrandParent = myNode.ParentNode.ParentNode.ParentNode;
Note that this applies to exceptions. For example, if the current node does not have a ParentNode (or is of type - Attribute, Document, DocumentFragment, Entity, Notation), then the code will throw an exception because such nodes cannot have parent elements.
Please edit your question to provide more information so that the answers can be "better".
a source to share