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. get parent and level of the xml node from XPathNavigator

get parent and level of the xml node from XPathNavigator

Scheduled Pinned Locked Moved C#
tutorialbusinessxmlquestionannouncement
5 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.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    Hi, For the below xml, I would like to get the parent and level of the entity node. For example, for the Business entity, token value should be BUSINESS; desc value should be Business News; parent should be PROD; level should be 1. From the below code, I'm getting token and desc values. Kindly guide me to get parent and level values. Thanks in advance.

    foreach (XPathNavigator book in topicsXml.CreateNavigator().Select("//Entity"))
    {
    string token = book.SelectSingleNode("Token").Value;
    string desc = book.SelectSingleNode("Description").Value;
    string parent = ?
    string level = ?
    }

    PROD
    prod\---->level 0
    BUSINESS
      Business News\---->level 1
      COS
        Company News\---->level 2
        ANA
          Analyst Ratings\---->level 3
          ANAMOVES
            Analyst Ratings, Estimates and Target Price Changes\---->level 4
            ANACHANGE
              Analyst Rating Changes\---->level 5
              ANACUT
                Analyst Downgrades\---->level 6
                ANACUTEVT
                  Analyst Ratings Cut Events, Announcements\---->level 7
                
              
            
          
          IP
            Intellectual Property\---->level 4
            COPYRIGHT
              Copyrights\---->level 5
            
          
        
      
      DRGPATENT
        Drug Patents\---->level 2
    
    M 1 Reply Last reply
    0
    • N NarVish

      Hi, For the below xml, I would like to get the parent and level of the entity node. For example, for the Business entity, token value should be BUSINESS; desc value should be Business News; parent should be PROD; level should be 1. From the below code, I'm getting token and desc values. Kindly guide me to get parent and level values. Thanks in advance.

      foreach (XPathNavigator book in topicsXml.CreateNavigator().Select("//Entity"))
      {
      string token = book.SelectSingleNode("Token").Value;
      string desc = book.SelectSingleNode("Description").Value;
      string parent = ?
      string level = ?
      }

      PROD
      prod\---->level 0
      BUSINESS
        Business News\---->level 1
        COS
          Company News\---->level 2
          ANA
            Analyst Ratings\---->level 3
            ANAMOVES
              Analyst Ratings, Estimates and Target Price Changes\---->level 4
              ANACHANGE
                Analyst Rating Changes\---->level 5
                ANACUT
                  Analyst Downgrades\---->level 6
                  ANACUTEVT
                    Analyst Ratings Cut Events, Announcements\---->level 7
                  
                
              
            
            IP
              Intellectual Property\---->level 4
              COPYRIGHT
                Copyrights\---->level 5
              
            
          
        
        DRGPATENT
          Drug Patents\---->level 2
      
      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      For the parent node, you can use .. to navigate upward the xml tree (like as it where a directory). Using your Example, string parent = book.SelectSingleNode("../Token").Value; will get you the parent token of the current entity. For the depth, you could try to count the number of entity parent nodes, like this: string level = book.SelectSingleNode("count(ancestor::Entity)").Value; I don't know if that will give you a 0-based or a 1-based value, you will have to do some tests.

      N 1 Reply Last reply
      0
      • M Mirko1980

        For the parent node, you can use .. to navigate upward the xml tree (like as it where a directory). Using your Example, string parent = book.SelectSingleNode("../Token").Value; will get you the parent token of the current entity. For the depth, you could try to count the number of entity parent nodes, like this: string level = book.SelectSingleNode("count(ancestor::Entity)").Value; I don't know if that will give you a 0-based or a 1-based value, you will have to do some tests.

        N Offline
        N Offline
        NarVish
        wrote on last edited by
        #3

        string parent = book.SelectSingleNode("../Token").Value;

        This returns the inner data of xml value of parent: "PRODprodBUSINESSBusiness NewsCOSCompany NewsANAAnalyst RatingsANAMOVESAnalyst Ratings, Estimates and Target Price ChangesANACHANGEAnalyst Rating ChangesANACUTAnalyst DowngradesANACUTEVTAnalyst Ratings Cut Events, AnnouncementsIPIntellectual PropertyCOPYRIGHTCopyrightsDRGPATENTDrug Patents"

        string level = book.SelectSingleNode("count(ancestor::Entity)").Value;

        This line thrown exception, "Expression must evaluate to a node-set." I hope I'll succeed in getting level by using ancestor, by giving correct syntax. Please let me know how to get the parent value. Thank you

        M 1 Reply Last reply
        0
        • N NarVish

          string parent = book.SelectSingleNode("../Token").Value;

          This returns the inner data of xml value of parent: "PRODprodBUSINESSBusiness NewsCOSCompany NewsANAAnalyst RatingsANAMOVESAnalyst Ratings, Estimates and Target Price ChangesANACHANGEAnalyst Rating ChangesANACUTAnalyst DowngradesANACUTEVTAnalyst Ratings Cut Events, AnnouncementsIPIntellectual PropertyCOPYRIGHTCopyrightsDRGPATENTDrug Patents"

          string level = book.SelectSingleNode("count(ancestor::Entity)").Value;

          This line thrown exception, "Expression must evaluate to a node-set." I hope I'll succeed in getting level by using ancestor, by giving correct syntax. Please let me know how to get the parent value. Thank you

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          For the parent, it seems you are selecting a not-leaf node (I'd say an 'Entity' node), so, using value, you are getting the full content of that node, all descendants included. If you want a string value, you must select a Token or a Description. For the level, I forgot you can't use scalar function in XPath when selecting nodes. You can simply use Select and then Count the result: int level = book.Select("ancestor::Entity").Count;

          N 1 Reply Last reply
          0
          • M Mirko1980

            For the parent, it seems you are selecting a not-leaf node (I'd say an 'Entity' node), so, using value, you are getting the full content of that node, all descendants included. If you want a string value, you must select a Token or a Description. For the level, I forgot you can't use scalar function in XPath when selecting nodes. You can simply use Select and then Count the result: int level = book.Select("ancestor::Entity").Count;

            N Offline
            N Offline
            NarVish
            wrote on last edited by
            #5

            Thank you, Mirko. Its working..

            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