Tag Archives: xpath

XPath and C#

Our C# application needs to retrieve data from a Java SOAP source. And, anyone who has dealt with SOAP for any time knows that standards aren’t. In our case, the null dates in the data were being passed back with a date that the Java system, I supposed, considered ‘null’: 1899-01-01 00:00:00. But our C# system parsed the incoming XML and saw a ‘Date Format Exception’.

This all happened when we used Visual Studio’s automagic object creator (Add Service Reference/Advanced/Add Web Reference). When we switched to used the ‘standard’ Add Service Reference, this error disappeared. But we then saw a Number Format Exception error. We couldn’t figure out where this one was coming from, so we started seeking other options.

The Java SOAP services are being created using Axis 2, which permits the user to also make REST calls. So I changed my code to use an XPathDocument to make a REST call for the data. The data returned is still in XML format. But this allows me to walk through the elements myself and either ignore the elements if I don’t need them, or handle a single element’s value if it causes issues.

I found about 5 different ways of accomplishing this exercise before settling on the XPathDocument method I found in Using Returned XML with C#. The one ‘gotcha’ that I had to contend with was remembering to use the XMLNamespaceManager once I created it. Using it is optional when searching for nodes.

The one ‘gotcha’ I ran into was remembering to include *all* the namespaces used within the document, and to use the optional namespace argument when searching for nodes. Without it, I ran into expected – and unexpected – errors:

1) The obvious one – it complained about the fact I was using ‘:’ within the node name but not declaring namespaces.
2) I ran into one error – I can’t reproduce it now – but it complained about the ‘:’ and only told me that was an illegal character to use within a node name, without telling me why.
3) I put one namespace in, but not both that were used in the document and ran into problems with that as well.