locked
Find and replace value in long xml data c# RRS feed

  • Question

  • User88744855 posted

    i am calling a web service which return many

    RatedShipment tag <rate:RatedShipment>

    now i need to find

    <rate:Code>11</rate:Code>

    if value is 11 then i need to change value of

    MonetaryValue

    in another tag call

    NegotiatedRateCharges
    NegotiatedRateCharges is child tag of RatedShipment tag

    anyone can help

    <rate:NegotiatedRateCharges>
        <rate:TotalCharge>
            <rate:CurrencyCode>EUR</rate:CurrencyCode>
            <rate:MonetaryValue>45.92</rate:MonetaryValue>
        </rate:TotalCharge>
    </rate:NegotiatedRateCharges>

    my sample xml snippet

    <rate:RatedShipment>
                    <rate:Service>
                        <rate:Code>11</rate:Code>
                        <rate:Description/>
                    </rate:Service>
                    <rate:RatedShipmentAlert>
                        <rate:Code>110971</rate:Code>
                        <rate:Description>Your invoice may vary from the displayed reference rates</rate:Description>
                    </rate:RatedShipmentAlert>
                    <rate:BillingWeight>
                        <rate:UnitOfMeasurement>
                            <rate:Code>KGS</rate:Code>
                            <rate:Description>Kilograms</rate:Description>
                        </rate:UnitOfMeasurement>
                        <rate:Weight>1.0</rate:Weight>
                    </rate:BillingWeight>
                    <rate:TransportationCharges>
                        <rate:CurrencyCode>EUR</rate:CurrencyCode>
                        <rate:MonetaryValue>46.38</rate:MonetaryValue>
                    </rate:TransportationCharges>
                    <rate:ServiceOptionsCharges>
                        <rate:CurrencyCode>EUR</rate:CurrencyCode>
                        <rate:MonetaryValue>0.00</rate:MonetaryValue>
                    </rate:ServiceOptionsCharges>
                    <rate:TotalCharges>
                        <rate:CurrencyCode>EUR</rate:CurrencyCode>
                        <rate:MonetaryValue>46.38</rate:MonetaryValue>
                    </rate:TotalCharges>
                    <rate:NegotiatedRateCharges>
                        <rate:TotalCharge>
                            <rate:CurrencyCode>EUR</rate:CurrencyCode>
                            <rate:MonetaryValue>45.92</rate:MonetaryValue>
                        </rate:TotalCharge>
                    </rate:NegotiatedRateCharges>
                    <rate:RatedPackage>
                        <rate:Weight>1.0</rate:Weight>
                    </rate:RatedPackage>
                </rate:RatedShipment>
    Monday, November 3, 2014 7:03 AM

Answers

  • User88744855 posted

    i got the solution and it is solved now. here is the code

    XDocument doc = XDocument.Parse(responseXml);
          XNamespace rate = "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1";
          XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
    
          //get RateShipment node where Service/Code = 11
          var ratedShipment = (from nodes in doc.Root.Elements(soapenv + "Body").Elements(rate + "RateResponse").Elements(rate + "RatedShipment")
                               where nodes.Elements(rate + "Service") != null && nodes.Elements(rate + "Service").Elements(rate + "Code") != null
                               && nodes.Elements(rate + "Service").Elements(rate + "Code").First().Value == "11"
                               select nodes).FirstOrDefault();
    
          //change the charges:
          var totalCharge = ratedShipment.Elements(rate + "NegotiatedRateCharges").Elements(rate + "TotalCharge").FirstOrDefault();
          totalCharge.Elements(rate + "CurrencyCode").FirstOrDefault().Value = "SEK";
          totalCharge.Elements(rate + "MonetaryValue").FirstOrDefault().Value = "10.00";
    
          string modifedXml = doc.ToString();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 3, 2014 10:04 AM