1

I can read the XML and return the Order Element.

I also need to return the ShippingAddress with is nested in the Order Element. (Name, AddressLine1, City, PostalCode, CountryCode, Phone) at the same time from the XML Response.

Please can someone show me how to reach the ShippingAddress at the same time.

My current code is

private static IEnumerable<FormsPersistence> Addresses(XContainer doc)
    {
        XNamespace ns = "https://mws.amazonservices.com/Orders/2013-09-01";

        return from address in doc.Descendants(ns + "Order")
               select new FormsPersistence
               {
                   AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
                   PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
                   BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
                   BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
                   OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
               };
    }

    private static void OrderDetails(IEnumerable<FormsPersistence> addresses)
    {


        foreach (var address in addresses)
        {
            Console.WriteLine("---");
            Console.WriteLine(address.AmazonOrderId);
            Console.WriteLine(address.PurchaseDate);
            Console.WriteLine(address.BuyerName);
            Console.WriteLine(address.BuyerEmail);
            Console.WriteLine(address.OrderStatus);
            Console.WriteLine("---");
        }
    }

The XML Response is

<ListOrdersResult>
<CreatedBefore>10/23/2017 14:09:56</CreatedBefore>
<Orders>
  <Order>
    <AmazonOrderId>026-000000-000000</AmazonOrderId>
    <PurchaseDate>10/20/2017 13:45:17</PurchaseDate>
    <LastUpdateDate>10/20/2017 14:16:38</LastUpdateDate>
    <OrderStatus>Unshipped</OrderStatus>
    <FulfillmentChannel>MFN</FulfillmentChannel>
    <SalesChannel>Amazon.co.uk</SalesChannel>
    <ShipServiceLevel>Std UK Dom_1</ShipServiceLevel>
    <ShippingAddress>
      <Name>kkkkk kkkk</Name>
      <AddressLine1>22 kkkkk road</AddressLine1>
      <City>London</City>
      <PostalCode>SW14 JBD</PostalCode>
      <CountryCode>GB</CountryCode>
      <Phone>0000000000</Phone>
    </ShippingAddress>
    <OrderTotal>
      <CurrencyCode>GBP</CurrencyCode>
      <Amount>46.00</Amount>
    </OrderTotal>
    <NumberOfItemsShipped>0</NumberOfItemsShipped>
    <NumberOfItemsUnshipped>6</NumberOfItemsUnshipped>
    <PaymentExecutionDetail />
    <PaymentMethod>Other</PaymentMethod>
    <PaymentMethodDetails>
      <PaymentMethodDetail>Standard</PaymentMethodDetail>
    </PaymentMethodDetails>
    <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
    <BuyerEmail>[email protected]</BuyerEmail>
    <BuyerName>kkk akkkk</BuyerName>
    <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
    <ShippedByAmazonTFM>False</ShippedByAmazonTFM>
    <OrderType>StandardOrder</OrderType>
    <EarliestShipDate>10/23/2017 00:00:00</EarliestShipDate>
    <LatestShipDate>10/24/2017 23:59:59</LatestShipDate>
    <EarliestDeliveryDate>10/25/2017 00:00:00</EarliestDeliveryDate>
    <LatestDeliveryDate>10/27/2017 23:59:59</LatestDeliveryDate>
    <IsBusinessOrder>False</IsBusinessOrder>
    <IsPrime>False</IsPrime>
    <IsPremiumOrder>False</IsPremiumOrder>
    <IsReplacementOrder>False</IsReplacementOrder>
  </Order>
</Orders>

The Class Files

class FormsPersistence
{
    public string AmazonOrderId { get; set; }
    public string MerchantOrderID { get; set; }
    public string PurchaseDate { get; set; }
    public string OrderStatus { get; set; }
    public string ASIN { get; set; }
    public string SKU { get; set; }
    public string ItemStatus { get; set; }
    public string ProductName { get; set; }
    public string Quantity { get; set; }
    public string BuyerName { get; set; }
    public string BuyerEmail { get; set; }
    public string Name { get; set; }
    public string AddressLine1 { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string CountryCode { get; set; }
    public string Phone { get; set; }
    public string ShippingAddress { get; set; }
}

and I added

class ShippingAddress
{
    public string Name { get; set; }
    public string AddressLine { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string CountryCode { get; set; }
    public string Phone { get; set; }
  }
2
  • But where you want to put the Shipping address in the result, is a property of FormsPersistence? Commented Oct 24, 2017 at 14:57
  • Yes I have them set in FormsPersitence like public string Name { get; set; } Commented Oct 24, 2017 at 14:59

2 Answers 2

1

Well supposing that FormsPersistence has a property to save that, you can do the following:

  return from address in doc.Descendants(ns + "Order")
         let shipping=address.Element(ns+"ShippingAddress")
         select new FormsPersistence
           {
               AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
               PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
               BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
               BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
               OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
               ShippingAddress= new ShippingAddress{Name=(string)shipping.Element(ns+"Name"),
                                                    AddressLine=(string)shipping.Element(ns+"AddressLine1"),
                                                   }
           };

You need to use Element method, which allows you to get a inner element of the current node, and with the help of let clause you can save the result to use it later in your projection as I show in the code above.

Sign up to request clarification or add additional context in comments.

12 Comments

I get The type or namespace name 'ShippingAddress' could not be found (are you missing a using directive or an assembly reference?) highlighted in VS on new ShippingAddress
Yes, I was supposing you have a property in your model with that name (which is a custom class), if you don't have it you can create it or add the properties to save the shipping information directly in you FormPersistence class
Sorry for my basic knowledge I'm learning as I go. I added a new class ShippingAddress with in this I added Name and AddressLine. I now get Cannot implicitly convert type 'AmazonMWS.src.MarketplaceWebServiceOrders.ShippingAddress' to 'string'
You need the namespace for shipping: let shipping=address.Element(ns + "ShippingAddress")
Look at the query, I'm creating a new instance of ShippingAddress: new ShippingAddress{}, I'm not doing directly ShippingAddress= (string)shipping.Element(...)
|
0

The trick is to use Elements and then use FirstOrDefault()

return doc.Descendants(ns + "Order").Select(address => new FormsPersistence()
               {
                   AmazonOrderId = (string)address.Element(ns + "AmazonOrderId").Value ?? string.Empty,
                   PurchaseDate = (string)address.Element(ns + "PurchaseDate").Value ?? string.Empty,
                   BuyerName = (string)address.Element(ns + "BuyerName").Value ?? string.Empty,
                   BuyerEmail = (string)address.Element(ns + "BuyerEmail").Value ?? string.Empty,
                   OrderStatus = (string)address.Element(ns + "OrderStatus").Value ?? string.Empty,
                   ShippingAddress = address.Elements(ns + "ShippingAddress").Select(shipping => new {
                        name = (string)shipping.Element(ns + "Name")
                   }).FirstOrDefault() 
               }).ToList();

Comments

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.