views:

429

answers:

2

I'm using a webservice which returns an XmlElement object to my c# program. I would like to read information from the XmlElement using Xpath. What is the best way to create an XPathDocument from the XmlElement?

+2  A: 

You can create a navigator directly against the XmlElement and use your xpath from there with the navigators Select* methods, no XPathDocument required.

annakata
Thanks for your answer :)
macleojw
+3  A: 

If you have an XmlElement, can you not just use SelectNodes() / SelectSingleNode()?

Also, all XmlNodes are IXPathNavigable, allowing you to get a navigator.

Finally, you can use new XmlNodeReader(element), and use this to create an XPathDocument using the overload that accepts an XmlReader.

Marc Gravell
huh, I never realised there was a select method immediately on xmlelement...
annakata
Thanks for your answer :).
macleojw