在這裏我粘貼從我嘗試提取值的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;
請告訴我在哪裏,我想提出的錯誤。感謝
感謝您的幫助。爲什麼ChildNodes [0] .Value不起作用? – Thomas 2014-11-04 14:45:57
可以指導我如何檢查common:在提取值之前,xml中是否存在描述? – Thomas 2014-11-04 14:48:20
你可以簡單地將selectedinglenode結果存儲到一個變量中,然後檢查,如果var爲null意味着在xml中沒有這樣的節點... – har07 2014-11-04 14:51:04