How to find Depth of XML file(no' of levels in XML file)
-
Hi all, I have a requirement of finding the depth of XML file. For that i wrote an Recursive function but its not giving the correct value. Please suggest me how to get the no'of levels in XML file or Depth of XML file. Thanks in advance.
-
Hi all, I have a requirement of finding the depth of XML file. For that i wrote an Recursive function but its not giving the correct value. Please suggest me how to get the no'of levels in XML file or Depth of XML file. Thanks in advance.
You could post your recursive function, this will be the best point to start.
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
You could post your recursive function, this will be the best point to start.
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
int depth = 0; XmlDocument doc = new XmlDocument("./XML/Sample.xml"); XmlNode node = doc.DocumentElement; XmlNodeList nodeList = node.DocumentElement.ChildNodes; if(nodeList.Count > 0) { depth ++; foreach(XmlNode cnode in nodeList) { Recursive(cnode); depth++; } } public void Recursive(XmlNode node) { if(node.HasChildNodes) { XmlNodeList nodeList = node.ChildNodes; if(nodeList.Count >0) { foreach(XmlNode cnode in nodeList) { Recursive(cnode); depth++; } } } }
-
int depth = 0; XmlDocument doc = new XmlDocument("./XML/Sample.xml"); XmlNode node = doc.DocumentElement; XmlNodeList nodeList = node.DocumentElement.ChildNodes; if(nodeList.Count > 0) { depth ++; foreach(XmlNode cnode in nodeList) { Recursive(cnode); depth++; } } public void Recursive(XmlNode node) { if(node.HasChildNodes) { XmlNodeList nodeList = node.ChildNodes; if(nodeList.Count >0) { foreach(XmlNode cnode in nodeList) { Recursive(cnode); depth++; } } } }
:) You are not counting the depth of the xml file, you are counting the nodes (not all, but nearly). What you have to do is sth similar to Depth-First-Search. You have to travel each path from root to leaf determining the length and check if this is bigger than all length of all paths in your tree. Found an example in Java, doing exactly what you need: http://www.sourcecodesworld.com/articles/java/java-data-structures/Determining_Tree_Depth.asp[^] Take a look at the method _getdepth(). What the code does is to get the depth for each subtree and then compares these values, checking if it is bigger than all seen before. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.