开发者

Problems working with LINQ to XML

开发者 https://www.devze.com 2023-03-15 11:34 出处:网络
I\'m trying to work with LINQ to XML to parse the notifications I\'m getting from Goog开发者_StackOverflow中文版le Checkout.

I'm trying to work with LINQ to XML to parse the notifications I'm getting from Goog开发者_StackOverflow中文版le Checkout.

The response is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<authorization-amount-notification xmlns="http://checkout.google.com/schema/2" serial-number="153286076708098-00005-6">
 <authorization-amount currency="USD">60.0</authorization-amount>
 <authorization-expiration-date>2011-07-03T21:27:48.000Z</authorization-expiration-date>
 <avs-response>Y</avs-response>
 <cvn-response>M</cvn-response>
 <timestamp>2011-06-26T21:28:48.741Z</timestamp>
 <google-order-number>153286076708098</google-order-number>
 <order-summary>
   <total-chargeback-amount currency="USD">0.0</total-chargeback-amount>
   <google-order-number>153286076708098</google-order-number>
   <total-charge-amount currency="USD">0.0</total-charge-amount>
   <total-refund-amount currency="USD">0.0</total-refund-amount>
   <risk-information>
     <ip-address>77.42.229.34</ip-address>
     <billing-address>
       <address1>somewhere in Beirut</address1>
       <address2></address2>
       <phone>70892555</phone>
       <email>Technical@fisharwe.com</email>
       <contact-name>Fisharwe User</contact-name>
       <company-name></company-name>
       <fax></fax>
       <country-code>LB</country-code>
       <city>Beirut</city>
       <region></region>
       <postal-code>1000</postal-code>
     </billing-address>
     <avs-response>Y</avs-response>
     <cvn-response>M</cvn-response>
     <eligible-for-protection>true</eligible-for-protection>
     <partial-cc-number>1111</partial-cc-number>
     <buyer-account-age>18</buyer-account-age>
   </risk-information>
   <authorization>
     <authorization-amount currency="USD">60.0</authorization-amount>
     <authorization-expiration-date>2011-07-03T21:27:48.000Z</authorization-expiration-date>
   </authorization>
   <purchase-date>2011-06-26T21:27:48.000Z</purchase-date>
   <archived>false</archived>
   <shopping-cart>
     <items>
       <item>
         <item-name>Credits</item-name>
         <item-description>Description</item-description>
         <unit-price currency="USD">60.0</unit-price>
         <quantity>1</quantity>
       </item>
     </items>
   </shopping-cart>
   <order-adjustment>
     <merchant-codes />
     <total-tax currency="USD">0.0</total-tax>
     <adjustment-total currency="USD">0.0</adjustment-total>
   </order-adjustment>
   <promotions />
   <buyer-id>975104325298289</buyer-id>
   <buyer-marketing-preferences>
     <email-allowed>false</email-allowed>
   </buyer-marketing-preferences>
   <buyer-shipping-address>
     <address1>somewhere in Beirut</address1>
     <address2></address2>
     <phone>70892555</phone>
     <email>Technical@fisharwe.com</email>
     <contact-name>Fisharwe User</contact-name>
     <company-name></company-name>
     <fax></fax>
     <structured-name>
       <first-name>Fisharwe</first-name>
       <last-name>User</last-name>
     </structured-name>
     <country-code>LB</country-code>
     <city>Beirut</city>
     <region></region>
     <postal-code>1000</postal-code>
   </buyer-shipping-address>
   <order-total currency="USD">60.0</order-total>
   <fulfillment-order-state>NEW</fulfillment-order-state>
   <financial-order-state>CHARGEABLE</financial-order-state>
 </order-summary>
</authorization-amount-notification>

Here's the code I'm using:

        var serverResponse = _checkoutService.Post(data, GoogleCheckoutConstants.ReportsUri);
        var xmlData = XDocument.Parse(serverResponse);
        bool charged = false;
        if(xmlData.Root.Name.Equals("authorization-amount-notification"))
        {

            var amount = (from c in xmlData.Elements()
                          where c.Name.Equals("authorization-amount")
                          select c).First().Value;
            var googleNumber = (from c in xmlData.Elements()
                                where c.Name.Equals("google-order-number")
                                select c).First().Value;
            _checkoutService.ChargeAndShip(googleNumber, amount);
            charged = true;
        }

This is the first time I use LINQ to XML, so I'm not really sure what's wrong with my code. But it's not even going inside the if statement. So when I replace the condition with:

if (serverResponse.IndexOf("authorization-amount-notification") > -1)

I end up getting errors telling me that the amount and googleNumber were not found.

Any suggestions?


You need to put the namespace in to the Xml, and you the Elements are SubElements of the Root Node.

You are only after one Element so doing Elements() then .First() is pointless. Just do Element() instead.

Also, you can match element names by passing in the Name of the Element + namespace to the Element() method.

var xmlData = XDocument.Parse(xml);

XNamespace ns = "http://checkout.google.com/schema/2";

if (xmlData.Root.Name == ns + "authorization-amount-notification")
{ 
    var amount = 
        xmlData
        .Root
        .Element(ns + "authorization-amount")
        .Value;

    var googleNumber = 
        xmlData
        .Root
        .Element(ns + "google-order-number")
        .Value;  
    _checkoutService.ChargeAndShip(googleNumber, amount);             

    charged = true;
}


What about...

if(xmlData.Root.Name.LocalName.Equals("new-order-notification")){
 .... 
}

But the xml you posted doesn't seem to match the code your using.. The elements do not exist

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号