从soapxml

时间:2016-07-29 11:16:16

标签: c# xml soap-client

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

从XML中提取错误:错误
<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什么都不返回。

1 个答案:

答案 0 :(得分:2)

你必须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之前

。确保将namespacemanager中的命名空间别名与XPATH表达式中使用的别名同步。