Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlDocument.SelectSingleNode and prefix + xmlNamespace issue

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...

like image 949
VSP Avatar asked Feb 23 '12 11:02

VSP


People also ask

What is SelectSingleNode?

SelectSingleNode(String) Selects the first XmlNode that matches the XPath expression. SelectSingleNode(String, XmlNamespaceManager) Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.

How do you add a prefix in XML?

When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is namespace manager?

The Namespace manager allows a user with administrative rights to create,edit, and delete namespaces and to change the settings of these namespaces.


1 Answers

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);
like image 119
dtb Avatar answered Sep 20 '22 04:09

dtb