如何从使用名称空间的xml文件中读取特定数据c#

本文关键字:读取 文件 数据 xml 空间 | 更新日期: 2023-09-27 18:28:57

通过这种方式,我试图读取类似CurrencyCode & MonetaryValue的值,但我在代码中遗漏了一些内容,因此我会出错。命名空间出现问题。我在这里粘贴我的代码。所以请尽可能纠正我的错误。感谢

string responseXml=@"<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:Response>
                    <ship:ShipmentResults>
                        <ship:NegotiatedRateCharges>
                            <ship:TotalCharge>
                                <ship:CurrencyCode>EUR</ship:CurrencyCode>
                                <ship:MonetaryValue>27.57</ship:MonetaryValue>
                            </ship:TotalCharge>
                        </ship:NegotiatedRateCharges>
                    </ship:ShipmentResults>
                </ship:ShipConfirmResponse>
            </soapenv:Body>
        </soapenv:Envelope>";
             XmlDocument xDoc = new XmlDocument();
             xDoc.LoadXml(responseXml);
             XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xDoc.NameTable);
             xmlnsManager.AddNamespace("ship:ShipConfirmResponse", "http://www.ups.com/XMLSchema/XOLTWS/Ship/v1.0");
             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 responseXml = @"<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>00xwst261bw4JVMGhQ9Ldx</common:TransactionIdentifier>
                            </common:TransactionReference>
                        </common:Response>
                        <ship:ShipmentResults>
                            <ship:ShipmentCharges>
                                <ship:TransportationCharges>
                                    <ship:CurrencyCode>EUR</ship:CurrencyCode>
                                    <ship:MonetaryValue>46.80</ship:MonetaryValue>
                                </ship:TransportationCharges>
                                <ship:ServiceOptionsCharges>
                                    <ship:CurrencyCode>EUR</ship:CurrencyCode>
                                    <ship:MonetaryValue>4.85</ship:MonetaryValue>
                                </ship:ServiceOptionsCharges>
                                <ship:TotalCharges>
                                    <ship:CurrencyCode>EUR</ship:CurrencyCode>
                                    <ship:MonetaryValue>51.65</ship:MonetaryValue>
                                </ship:TotalCharges>
                            </ship:ShipmentCharges>
                            <ship:NegotiatedRateCharges>
                                <ship:TotalCharge>
                                    <ship:CurrencyCode>EUR</ship:CurrencyCode>
                                    <ship:MonetaryValue>27.57</ship:MonetaryValue>
                                </ship:TotalCharge>
                            </ship:NegotiatedRateCharges>
                            <ship:BillingWeight>
                                <ship:UnitOfMeasurement>
                                    <ship:Code>KGS</ship:Code>
                                    <ship:Description>Kilograms</ship:Description>
                                </ship:UnitOfMeasurement>
                                <ship:Weight>1.0</ship:Weight>
                            </ship:BillingWeight>
                            <ship:ShipmentIdentificationNumber>1Z4V4F249996458173</ship:ShipmentIdentificationNumber>
                            <ship:ShipmentDigest></ship:ShipmentDigest>
                        </ship:ShipmentResults>
                    </ship:ShipConfirmResponse>
                </soapenv:Body>
            </soapenv:Envelope>";
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(responseXml);
            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");
            string sCurrencyCode = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/ship:ShipmentResults/ship:NegotiatedRateCharges/ship:TotalCharge/ship:CurrencyCode", xmlnsManager).InnerText;
            string sMonetaryValue = xDoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/ship:ShipConfirmResponse/ship:ShipmentResults/ship:NegotiatedRateCharges/ship:TotalCharge/ship:MonetaryValue", xmlnsManager).InnerText;

如何从使用名称空间的xml文件中读取特定数据c#

如果您只对CurrencyCode和MonetaryValue节点感兴趣,那么您可以使用XDocument查看Descendants,以确定它是否存在。

var doc = XDocument.Parse(responseXml);
// Element now has the two nodes CurrencyCode and MonetaryValue.
var elements = doc.Descendants().Where(d => d.Name.LocalName == "CurrencyCode" || d.Name.LocalName == "MonetaryValue");