Problem XmlDocument.SelectSingleNode and Prefix + xmlNamespace

I have the following line loaded into an XML document:

<?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Server</faultcode> <faultstring>El cliente con los parámetros introducidos no existe./faultstring> <detail> <ns:ClienteWSDo29Exception xmlns:ns="http://services.do29.imq.es"> <Do29Exception xmlns="http://services.do29.imq.es" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax211="http://model.do29.imq.es/xsd" xmlns:ax213="http://dto.do29.imq.es/xsd" xmlns:ax29="http://exception.do29.imq.es/xsd" xsi:type="ax29:Do29Exception"> <ax29:classname>class es.imq.do29.dao.ClienteDaoImpl</ax29:classname> <ax29:trace xsi:nil="true" /> <ax29:previous xsi:nil="true" /> <ax29:method>getCliente</ax29:method> <ax29:id>1</ax29:id> <ax29:message>El cliente con los parámetros introducidos no existe.</ax29:message> </Do29Exception> </ns:ClienteWSDo29Exception> </detail> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope> 

Then, following the example with namespaces in xml, I tried:

 XmlDocument xmldocu = new XmlDocument(); xmldocu.LoadXml(xml); XmlNamespaceManager namespaces = new XmlNamespaceManager(xmldocu.NameTable); namespaces.AddNamespace("ax29", "http://services.do29.imq.es"); XmlNode nodemsgx = xmldocu.SelectSingleNode("//message", namespaces); XmlNode nodemsg = xmldocu.SelectSingleNode("//ax29:message", namespaces); 

But nodemsgx and nodemsg are null: S What is the correct way to do this? I used the // message because I want to get any node of this type, and not a specific path to this node ...

+4
source share
2 answers

The ax29 prefix in the XML document is set to the namespace "http://exception.do29.imq.es/xsd" , not "http://services.do29.imq.es" .

Try the following:

 namespaces.AddNamespace("ax29", "http://exception.do29.imq.es/xsd"); XmlNode nodemsg = xmldocu.SelectSingleNode("//ax29:message", namespaces); 
+7
source

It is hard to say for us, since we don’t know what resul contains, but I assume that it contains xmldocu?

Have you tried to get any other elements from the document?

does XmlNode nodemsg = xmldocu.SelectSingleNode ("detail // ax29: message"); bring out something?

0
source

Source: https://habr.com/ru/post/1398013/


All Articles