Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectNodes not scoped to the Element

Tags:

.net

xml

xpath

If I call SelectNodes on an XmlElement, and pass XPath query such as this:

XmlNodeList nodes = xmlElement.SelectNodes("//OtherNode");

The nodes list will be for all the OtherNode elements in the document not just the ones from xmlElement.

I seem to recall that this is by design, and for a good reason, but I can't remember what that good reason was, nor how to get around it.

like image 272
Ralph Shillington Avatar asked Sep 25 '09 15:09

Ralph Shillington


1 Answers

Just add a dot to the beginning of the xpath. The dot selects the current node:

XmlNodeList nodes = xmlElement.SelectNodes(".//OtherNode");
like image 138
bruno conde Avatar answered Sep 28 '22 08:09

bruno conde