尝试使用命名空间c#从我的xml中提取值

时间:2014-11-04 14:08:39

标签: c# xml xpath

这里我将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;
请告诉我我在哪里弄错了。感谢

2 个答案:

答案 0 :(得分:1)

我认为你必须在xml路径的尾部省略'/',即

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

答案 1 :(得分:0)

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

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