0

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;
2
  • What is happening? Is there an error or just no return value? Commented Nov 3, 2014 at 13:53
  • You should take a look at this answer : stackoverflow.com/a/12897051/2284240 Commented Nov 3, 2014 at 13:53

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

what will be stored in elements variable?
element will have all the CurrencyCode and MonetaryValue elements from your xml. You can go through a foreach loop to read each one and access .Value property to get node value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.