【问题标题】:XmlDocument.SelectSingleNode and prefix + xmlNamespace issueXmlDocument.SelectSingleNode 和前缀 + xmlNamespace 问题
【发布时间】:2012-03-13 18:44:21
【问题描述】:

我将以下字符串加载到 XML 文档中:

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

然后按照我尝试的xml中命名空间的情况:

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);

但是 nodemsgx 和 nodemsg 是空的:S 正确的方法是什么? 我使用 //message 因为我想获取该类型的任何节点而不是该节点的特定路径...

【问题讨论】:

  • 请不要在标题前加上“c#”之类的前缀。这就是标签的用途。

标签: c# xml xpath selectnodes


【解决方案1】:

XML 文档中的ax29 前缀设置为"http://exception.do29.imq.es/xsd" 命名空间,而不是"http://services.do29.imq.es"

试试这个:

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

【讨论】:

  • 谢谢你搞定它...(有这么多的命名空间我被误导了...)
【解决方案2】:

很难告诉我们,因为我们不知道 resul 包含什么,但我猜它包含 xmldocu?

您是否尝试过从文档中获取任何其他元素?

确实 XmlNode nodemsg = xmldocu.SelectSingleNode("detail//ax29:message");有什么收获吗?

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多