Thomas
Thomas

Reputation: 34188

How to read a specific data from xml file where namespace is used c#

this way i am trying to read values like for CurrencyCode & MonetaryValue but i am missing something in code for which i am getting error. the problem occurring for namespace. here i am pasting my code. so please rectify me i possible where i am making the mistake. thanks

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;

UPDATE

i have managed to do it.

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;

Upvotes: 0

Views: 138

Answers (1)

loopedcode
loopedcode

Reputation: 4893

If you are only interested to get CurrencyCode and MonetaryValue node, then you can just use XDocument to look into the Descendants to find if its there.

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");

Upvotes: 1

Related Questions