0

I'm trying to utilise a web service/method which accepts an XmlDocument and returns an XmlDocument. When I add a reference to this web service to my C# application, the proxy code is defined to use XmlNodes instead of XmlDocuments. This seems fairly well recognised behaviour (but confirmation and/or an explanation would be nice - see SO here).

However, if I take the returned XmlNode object and try and do a simple XPath query to "SelectNodes(XPath)", I get no nodes found - but matching nodes do exist.

If, however, I take the OuterXml of the returned XmlNode, and create myself a new XmlDocument from it, my XPath query finds the nodes I expected.

Why is that ? What is it about the returned XmlDocument/XmlNode (or conversion between) that stops the XPath query from working as expected ?

Thanks.

Edit: The Xml looks like this:

<Results xmlns="">
  <Result>
     :
    <Success>True</Success>
  </Result>
  <Result>
     :
    <Success>False</Success>
  </Result>
</Results>

...and the XPath looks like this...

Dim xPath As String = "//Result[Success='False']"

If (xmlResults.SelectNodes(xPath).Count > 0) Then
    Throw New ApplicationException("Results returned indicate a problem:- " + xmlResults.OuterXml)
End If
Community
  • 1
  • 1
Black Light
  • 2,358
  • 5
  • 27
  • 49
  • How does the XPath you tried look? How does the XML look? If you have a parentless `XmlNode` then perhaps paths starting with `/` work differently than with an `XmlDocument`. Or if you have an `XmlElement` returned with `bar` then of course a path would need to be relative to the `root` element so you would use a path like `root.SelectNodes("foo")` to find all `foo` child elements of the returned `root` element whilst with a complete XmlDocument you would use `doc.SelectNodes("root/foo")` or `doc.SelectNodes("/root/foo")`. – Martin Honnen Sep 11 '13 at 16:26

1 Answers1

0

One alternative to creating my own XmlDocument would be to use Linq, as per:

Dim results As XDocument = XDocument.Parse(xmlResults.OuterXml)

If (results.Descendants("Success").Count(Function(node) node.Value.ToLower() = "false") > 0) Then
    Throw New ApplicationException("Results returned indicate a problem:- " + xmlResults.OuterXml)
End If
Black Light
  • 2,358
  • 5
  • 27
  • 49