从 soapxml 获取 XML 个子节点值

Get XML Child Node Value from soapxml

从最近几天开始,我试图使用 c#

从关注 XML 中提取 err:Errors

我正在使用 UPS 网络服务,当我取消取件时,我收到此 XML 作为 return

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header />
  <soapenv:Body>
    <soapenv:Fault>
      <faultcode>Client</faultcode>
      <faultstring>An exception has been raised as a result of client data.</faultstring>
      <detail>
        <err:Errors xmlns:err="http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1">
          <err:ErrorDetail>
            <err:Severity>Hard</err:Severity>
            <err:PrimaryErrorCode>
              <err:Code>9510131</err:Code>
              <err:Description>Order has already been canceled</err:Description>
            </err:PrimaryErrorCode>
          </err:ErrorDetail>
        </err:Errors>
      </detail>
    </soapenv:Fault>
  </soapenv:Body>
</soapenv:Envelope>

但找不到任何相关解决方案。

这是我试过的:

XmlDocument xDoc = new XmlDocument(); 
xDoc.LoadXml(strResponse); 
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xDoc.NameTable);
xDoc.SelectSingleNode(
     "soapenv:Envelope/soap‌​env:Body/soapenv:Fault/err:ErrorDetail",
      xmlnsManager).InnerText;

看起来 SelectSingleNode return什么都没有。

您必须 add the namespaces 到您的 NamespaceManager:

xmlnsManager.AddNamespace("soapenv","http://schemas.xmlsoap.org/soap/envelope/");
xmlnsManager.AddNamespace("err","http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1");

xDoc.SelectSingleNode(
 "/soapenv:Envelope/soap‌​env:Body/soapenv:Fault/err:ErrorDetail",
  xmlnsManager).InnerText;

在您致电 SelectSingleNode 之前。确保将命名空间管理器中的命名空间别名与 XPATH 表达式中使用的别名同步。