2014-11-04 50 views
0

在这里我粘贴从我尝试提取值的xml。试图从命名空间c中提取我的xml值#

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ship:ShipConfirmResponse xmlns:ship="http://www.ups.com/XMLSchema/XOLTWS/Ship/v1.0"> 
      <common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"> 
       <common:ResponseStatus> 
        <common:Code>1</common:Code> 
        <common:Description>Success</common:Description> 
       </common:ResponseStatus> 
       <common:TransactionReference> 
        <common:CustomerContext/> 
        <common:TransactionIdentifier>werqqa</common:TransactionIdentifier> 
       </common:TransactionReference> 
      </common:Response> 
      <ship:ShipmentResults> 
       <ship:NegotiatedRateCharges> 
        <ship:TotalCharge> 
         <ship:CurrencyCode>EUR</ship:CurrencyCode> 
         <ship:MonetaryValue>15.50</ship:MonetaryValue> 
        </ship:TotalCharge> 
       </ship:NegotiatedRateCharges> 
      </ship:ShipmentResults> 
     </ship:ShipConfirmResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我需要找到Success词是有与否,如果找到的话,我将获取其他数据像货币和货币价值。

这是我用来达到目标​​的代码。我想我在哪里犯了一个错误,哪个成功价值没有被提取出来,而是被解雇了。

 XmlDocument xDoc = new XmlDocument(); 
     xDoc.LoadXml(strResponse); 
     XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xDoc.NameTable); 
     xmlnsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); 
     xmlnsManager.AddNamespace("ship", "http://www.ups.com/XMLSchema/XOLTWS/Ship/v1.0"); 
     xmlnsManager.AddNamespace("common", "http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"); 

     string strHasSuccess = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/common:Response/common:ResponseStatus/common:Description/", xmlnsManager).ChildNodes[0].Value; 

     string sCurrencyCode = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/ship:ShipmentResults/ship:NegotiatedRateCharges/ship:TotalCharge/ship:CurrencyCode/", xmlnsManager).ChildNodes[0].Value; 
     string sMonetaryValue = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/ship:ShipmentResults/ship:NegotiatedRateCharges/ship:TotalCharge/ship:MonetaryValue/", xmlnsManager).ChildNodes[0].Value; 

该行没有工作

string strHasSuccess = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/common:Response/common:ResponseStatus/common:Description/", xmlnsManager).ChildNodes[0].Value; 

请告诉我在哪里,我想提出的错误。感谢

回答

0

如果是我,我会请从XPath的尾随/,并使用InnerText代替ChildNodes[0].Value

string strHasSuccess = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/common:Response/common:ResponseStatus/common:Description", xmlnsManager) 
          .InnerText; 
+0

感谢您的帮助。为什么ChildNodes [0] .Value不起作用? – Thomas 2014-11-04 14:45:57

+0

可以指导我如何检查common:在提取值之前,xml中是否存在描述? – Thomas 2014-11-04 14:48:20

+0

你可以简单地将selectedinglenode结果存储到一个变量中,然后检查,如果var为null意味着在xml中没有这样的节点... – har07 2014-11-04 14:51:04

1

我认为你必须在你的XML路径的尾部省略“/”,即

string strHasSuccess = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/common:Response/common:ResponseStatus/common:Description", xmlnsManager).ChildNodes[0].Value; 
+0

确定它是排序的。你可以指导我如何检查common:在提取值之前,xml中是否存在描述? – Thomas 2014-11-04 14:50:25

+0

@Thomas为什么不使用linq-to-xml代替xml路径?用linq做这样的事情会容易很多。 – 2014-11-04 14:52:03