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 the ancestor nodes from XML

Get the ancestor nodes from XML

Scheduled Pinned Locked Moved C#
tutorialbusinessxmlannouncementlearning
4 Posts 3 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, when I select a particular node in the xml, I should be able to display all its ancestors's Token values. For example, for the Token:"ANA", output should be "PROD,BUSINESS,COS" for the Token:"DRGPATENT", output should be "PROD,BUSINESS" I tried to get the immediate parent by using below code. Pls guide me to get all its ancestors. Thanks in advance

    foreach (XPathNavigator book in topicsXml.CreateNavigator().Select("//Entity"))
    {
    string parent = book.SelectSingleNode("../Token").Value;
    }
    <CodeList>
    <Entity>
    <Token>PROD</Token>
    <Description>prod</Description>
    <Entity>
    <Token>BUSINESS</Token>
    <Description>Business News</Description>
    <Entity>
    <Token>COS</Token>
    <Description>Company News</Description>
    <Entity>
    <Token>ANA</Token>
    <Description>Analyst Ratings</Description>
    <Entity>
    <Token>ANAMOVES</Token>
    <Description>Analyst Ratings, Estimates and Target Price Changes</Description>
    <Entity>
    <Token>ANACHANGE</Token>
    <Description>Analyst Rating Changes</Description>
    <Entity>
    <Token>ANACUT</Token>
    <Description>Analyst Downgrades</Description>
    <Entity>
    <Token>ANACUTEVT</Token>
    <Description>Analyst Ratings Cut Events, Announcements</Description>
    </Entity>
    </Entity>
    </Entity>
    </Entity>
    <Entity>
    <Token>IP</Token>
    <Description>Intellectual Property</Description>
    <Entity>
    <Token>COPYRIGHT</Token>
    <Description>Copyrights</Description>
    </Entity>
    </Entity>
    </Entity>
    </Entity>
    <Entity>
    <Token>DRGPATENT</Token>
    <Description>Drug Patents</Description>
    </Entity>
    </Entity>
    </Entity>
    </CodeList>

    modified on Thursday, July 21, 2011 5:25 AM

    M P 2 Replies Last reply
    0
    • N NarVish

      Hi, when I select a particular node in the xml, I should be able to display all its ancestors's Token values. For example, for the Token:"ANA", output should be "PROD,BUSINESS,COS" for the Token:"DRGPATENT", output should be "PROD,BUSINESS" I tried to get the immediate parent by using below code. Pls guide me to get all its ancestors. Thanks in advance

      foreach (XPathNavigator book in topicsXml.CreateNavigator().Select("//Entity"))
      {
      string parent = book.SelectSingleNode("../Token").Value;
      }
      <CodeList>
      <Entity>
      <Token>PROD</Token>
      <Description>prod</Description>
      <Entity>
      <Token>BUSINESS</Token>
      <Description>Business News</Description>
      <Entity>
      <Token>COS</Token>
      <Description>Company News</Description>
      <Entity>
      <Token>ANA</Token>
      <Description>Analyst Ratings</Description>
      <Entity>
      <Token>ANAMOVES</Token>
      <Description>Analyst Ratings, Estimates and Target Price Changes</Description>
      <Entity>
      <Token>ANACHANGE</Token>
      <Description>Analyst Rating Changes</Description>
      <Entity>
      <Token>ANACUT</Token>
      <Description>Analyst Downgrades</Description>
      <Entity>
      <Token>ANACUTEVT</Token>
      <Description>Analyst Ratings Cut Events, Announcements</Description>
      </Entity>
      </Entity>
      </Entity>
      </Entity>
      <Entity>
      <Token>IP</Token>
      <Description>Intellectual Property</Description>
      <Entity>
      <Token>COPYRIGHT</Token>
      <Description>Copyrights</Description>
      </Entity>
      </Entity>
      </Entity>
      </Entity>
      <Entity>
      <Token>DRGPATENT</Token>
      <Description>Drug Patents</Description>
      </Entity>
      </Entity>
      </Entity>
      </CodeList>

      modified on Thursday, July 21, 2011 5:25 AM

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

      Remember when I told you to use int level = book.Select("ancestor::Entity").Count to get the level of a node? What ancestor::Entity does is returning the list of all the ancestor Entity nodes, you just have to avoid using Count. If you need the token value, you just have to subsequently select Token, like this: ancestor::Entity/Token. To understand what you can do with XPath, I strongly suggest you to read here.

      N 1 Reply Last reply
      0
      • M Mirko1980

        Remember when I told you to use int level = book.Select("ancestor::Entity").Count to get the level of a node? What ancestor::Entity does is returning the list of all the ancestor Entity nodes, you just have to avoid using Count. If you need the token value, you just have to subsequently select Token, like this: ancestor::Entity/Token. To understand what you can do with XPath, I strongly suggest you to read here.

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

        I tried as below "../Descritpion" gives immediate parent description; "../../Description" gives grandparent's description. and so on..

        for (int i = 0; i < level; i++)
        {
        temp+="../";
        parent += book.SelectSingleNode(temp + "Description").Value;
        }

        I tried book.Select("ancestor::Entity"), as you suggested. both are working fine. I think first one is not a good programming practice. Thank you for giving me the link.

        1 Reply Last reply
        0
        • N NarVish

          Hi, when I select a particular node in the xml, I should be able to display all its ancestors's Token values. For example, for the Token:"ANA", output should be "PROD,BUSINESS,COS" for the Token:"DRGPATENT", output should be "PROD,BUSINESS" I tried to get the immediate parent by using below code. Pls guide me to get all its ancestors. Thanks in advance

          foreach (XPathNavigator book in topicsXml.CreateNavigator().Select("//Entity"))
          {
          string parent = book.SelectSingleNode("../Token").Value;
          }
          <CodeList>
          <Entity>
          <Token>PROD</Token>
          <Description>prod</Description>
          <Entity>
          <Token>BUSINESS</Token>
          <Description>Business News</Description>
          <Entity>
          <Token>COS</Token>
          <Description>Company News</Description>
          <Entity>
          <Token>ANA</Token>
          <Description>Analyst Ratings</Description>
          <Entity>
          <Token>ANAMOVES</Token>
          <Description>Analyst Ratings, Estimates and Target Price Changes</Description>
          <Entity>
          <Token>ANACHANGE</Token>
          <Description>Analyst Rating Changes</Description>
          <Entity>
          <Token>ANACUT</Token>
          <Description>Analyst Downgrades</Description>
          <Entity>
          <Token>ANACUTEVT</Token>
          <Description>Analyst Ratings Cut Events, Announcements</Description>
          </Entity>
          </Entity>
          </Entity>
          </Entity>
          <Entity>
          <Token>IP</Token>
          <Description>Intellectual Property</Description>
          <Entity>
          <Token>COPYRIGHT</Token>
          <Description>Copyrights</Description>
          </Entity>
          </Entity>
          </Entity>
          </Entity>
          <Entity>
          <Token>DRGPATENT</Token>
          <Description>Drug Patents</Description>
          </Entity>
          </Entity>
          </Entity>
          </CodeList>

          modified on Thursday, July 21, 2011 5:25 AM

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          That looks familiar. :~

          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