XmlDocument.SelectSingleNode and prefix + xmlNames

2019-06-24 07:55发布

问题:

I have the following string loaded to 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 case 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 Whats the correct way to do it? I used //message becouse i want to get any node of that type not the specific path to this node...

回答1:

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

Try this:

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


回答2:

Hard to tell for us since we don't know what resul contains, but I'm guessing it contains xmldocu?

Have you tried getting any other elements from the document?

does XmlNode nodemsg = xmldocu.SelectSingleNode("detail//ax29:message"); yield anything?