2014-11-04 52 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