Get the ancestor nodes from XML
-
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
-
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
Remember when I told you to use
int level = book.Select("ancestor::Entity").Count
to get the level of a node? Whatancestor::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. -
Remember when I told you to use
int level = book.Select("ancestor::Entity").Count
to get the level of a node? Whatancestor::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.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.
-
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
That looks familiar. :~