Reputation: 1002
I'm trying to look up the ancestors of a certain node. Let's call it "NodeA". I get a list of "NodeA" nodes and want to look up its ancestors to check a certain attribute value.Here's my code to retrieve all "NodeA" nodes:
String xPathExpression = "//*[local-name()='NodeA']";
XPathNodeIterator nodeSet = (XPathNodeIterator)navigator.Evaluate(xPathExpression);
foreach (XPathNavigator item in nodeSet)
{
// Iterate through ancestors in here.
}
In the "// Iterate through ancestors in here" area, I can evaluate my node through an expression and get the list of ancestors the following way (for each "NodeA"):
XPathNavigator item;
var ancestorExpression = string.Format("//*[name()='{0}']/ancestor-or-self::*", item.Name);
XPathNodeIterator ancestors = (XPathNodeIterator)navigator.Evaluate(ancestorExpression);
However, this seems redundant, and since I have the original xml, and an XPathNavigator instance, can I not just get my ancestors at that point without having to evaluate an expression through the navigator? Something like the following (where item is of the type XPathNavigator):
item.GetAncestors();
or
Helper.GetAncestors(fullXML, item)
I realize there's a SelectAncestors method on the item, but I haven't had luck getting it to work. But if that is it, an example would be much appreciated
<NodeAGrandDaddy>
<NodeADaddy>
<NodeA>
<NodeA/>
<NodeADaddy/>
<NodeAGrandDaddy/>
Upvotes: 2
Views: 541
Reputation: 56162
Ok, well, prob you can use Select
method with ancestor-or-self::*
, i.e.:
item.Select("ancestor-or-self::*")
Upvotes: 2