I have the following c# method, that performs some operation on all nodes reachable from refNode, through xpath
void foo(XmlNode refNode, string xpath)
{
XmlNodeList list=refNode.SelectNodes(xpath);
//perform operation on each element of the list
}
One of the input xml that i'm getting is:
<A>
<B>***
<C>
<B>One</B>
</C>
<B>
<B>Two</B>
</B>
</B>
<B>...</B>
<B>...</B>
</A>
where i need to select a refNode <B>
(marked ***
) and pass it to foo() with an xpath that selects all descendent <B>
nodes of refNode, but not nested inside any other <B>
node
for example in the given input the result should contain:
1. <B>One</B>
2. <B><B>Two</B></B>
I have tried .//B which gives me 3 results and .//B[not(ancesotr::B)] which returns 0 results.
What Xpath should I use to get the desired result?
Edit
I can make changes to method foo, but not its signature. This method is a part of a library and is being used by few users. The input given above is just a specific instance, the user might also send node A as refnode and ask for all C nodes.
Edit2
@Dimitre Novatchev's solution works for me if I can get the xpath of the refnode inside foo without changing its signature or if there is some way to specify this
node, i.e. the node on which the xpath is being applied.
.//B[not(ancesotr::B) or ancesotr::B[1]=**this**]