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