XML DOM get current position Node
-
When i search a certain xml-file using "selectNodes("//ROOT/BLA")" and i receive the found nodes. Is it possible to get iterate through those found nodes and retrieve their current position in the xml-tree? (remember that this is DOM and not XSLT, so "position()" and all that crap won't work). Any help is greatly appriciated.
-
When i search a certain xml-file using "selectNodes("//ROOT/BLA")" and i receive the found nodes. Is it possible to get iterate through those found nodes and retrieve their current position in the xml-tree? (remember that this is DOM and not XSLT, so "position()" and all that crap won't work). Any help is greatly appriciated.
by position I presume you mean name - you could use:
XmlNodeList results = document.SelectNodes(""//root/bla"); foreach (XmlNode result in rtesults) { Console.Write("found {0} value = '{1}'", GetNodeName(result), result.value); } string GetNodeName(XmlNode node) { string result = node.LocalName; if (node.Parent != null) result = GetNodeName(node.Parent) + "/" + result; return result; }
if you want the position then just change the above function to count previousSiblings:string GetNodePosition(XmlNode node) { int previousSiblings = 0; for (XmlNode previous_node = node.PreviousSibling; previous_node != null; previous_node = previous_node.PreviousSibling) previousSiblings++; string result = previousSiblings.ToString(); if (node.Parent != null) result = GetNodeName(node.Parent) + "/" + result; return result; }
hope this helps
"When the only tool you have is a hammer, a sore thumb you will have."
-
by position I presume you mean name - you could use:
XmlNodeList results = document.SelectNodes(""//root/bla"); foreach (XmlNode result in rtesults) { Console.Write("found {0} value = '{1}'", GetNodeName(result), result.value); } string GetNodeName(XmlNode node) { string result = node.LocalName; if (node.Parent != null) result = GetNodeName(node.Parent) + "/" + result; return result; }
if you want the position then just change the above function to count previousSiblings:string GetNodePosition(XmlNode node) { int previousSiblings = 0; for (XmlNode previous_node = node.PreviousSibling; previous_node != null; previous_node = previous_node.PreviousSibling) previousSiblings++; string result = previousSiblings.ToString(); if (node.Parent != null) result = GetNodeName(node.Parent) + "/" + result; return result; }
hope this helps
"When the only tool you have is a hammer, a sore thumb you will have."