tags:

views:

106

answers:

3

Hi, If I have XML tree like this-

<Parent>
 <Image Name="a"/>
 <Image Name="b"/>
 <Child>
  <Image Name="c"/>
  <Image Name="d"/>
 </Child>
 <SomeElem>
  <Image Name="h"/>
  <Image Name="g"/>
 </SomeElem>
 <Image Name="e"/>
</Parent>

I want to select all <Image\> nodes except those listed inside <Child\> node. Currently I am using query to select all Image nodes, -

xElement.XPathSelectElements("//ns:Image", namespace);

Thanks in advance.

A: 

If you only want to select those at the root level, then you can just do:

xElement.XPathSelectElements("/ns:Image", namespace);

The // tells the xpath engine to look at all Image nodes, no matter their depth.

Kaleb Pederson
No I want all the child Image elements no matter if its root level or child's child.
Cool
Although I could change my answer to match your edits, @Anurag's answer already addresses that case.
Kaleb Pederson
Thanks a lot Kaleb. Sorry, but I had to edit my question as it was confusing and not precise.
Cool
No problem. I was being sufficiently lazy not to answer what I thought you may have intended ;).
Kaleb Pederson
lol..but anyways you sufficiently answered what my question implied ;)
Cool
A: 

/ns:Parent/ns:Image should do the trick

Matti Virkkunen
The Image element can be anywhere in the xml. Parent/Image, Parent/ABCD/Image, Parent/../../Image.I want all those Image elements except inside Child i.e. Parent/Child.
Cool
+2  A: 
Anurag
haha.. love the icons for today :D
Anurag
No, its still not working :( with //Image[not(ancestor::Child)]or with xElement.XPathSelectElements("//*[not(self::Child)]//ns:Image", namespace)
Cool
the xpath looks correct, here's an online tool you could use for testing - http://www.mizar.dk/XPath/Default.aspx. Is xElement pointing to a correct node?
Anurag
Hey Anurag, yes //Image[not(ancestor::Child)] this is working fine on ur link as well as in my code :) Thanks.
Cool