Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to find Depth of XML file(no' of levels in XML file)

How to find Depth of XML file(no' of levels in XML file)

Scheduled Pinned Locked Moved C#
xmltutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • K K V Sekhar

      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.

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • S SeMartens

        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.

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        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++; } } } }

        S 1 Reply Last reply
        0
        • K K V Sekhar

          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++; } } } }

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          :) 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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups