Hi,
I'm trying to write a routine to extract results from a web service written
in VS 2005 using XPath, however VS doesn't put a prefix on result namespaces
by default, so in the output from a service such as
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MyTest5Response xmlns="adam">
<MyTest5Result>
<testInt>9</testInt>
<testStruct>
<z>
<MyStruct><x>0</x><y>0</y><z>0</z></MyStruct>
<MyStruct><x>0</x><y>0</y><z>0</z></MyStruct>
</z>
</testStruct>
</MyTest5Result>
</MyTest5Response>
</soap:Body>
</soap:Envelope>
I can not use XPath to extract anything lower than soap:Body by name, e.g.
/soap:Envelope/soap:Body/MyTest5Response is invalid as MyTest5Response has no
namespace prefix.
Does anyone know if either
1) I'm wrong, and there is a valid XPath to the response / results nodes
2) It's possible to specify a prefix which will be output by the web service
3) There's an easy XSLT transform I could apply after calling the web
service to add a prefix (I have no knowledge of transforms myself); this
would be better, as I'll also have to interface with services written by
other companies.
The consumer program is not being produced in any VS languages, so I can't
use anything like XmlNamespaceManager or other .Net / VS features.
Thanks in advance,
Adam
John Saunders - 16 Oct 2006 18:23 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> <soap:Body>
> <MyTest5Response xmlns="adam">
You need to create an XmlNamespaceManager object and add the namespace to
it. Use whatever prefix you like when you add it. You then use that prefix
in your XPath queries. This is the same technique you'd use if the XML had
included a prefix. For instance, if you need to reference the <soap:Body>
element, you can do:
XmlNamespaceManager ns = new XmlNamespaceManager();
ns.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNode body = document.SelectSingleNode("//SOAP-ENV:Body", ns);
John
P.S. Always remember that prefixes only mean what someone decides they mean.
They always are just an abbreviation of a namespace. It's the namespace that
matters.