Reputation: 53
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; }
}
Upvotes: 1
Views: 181
Reputation: 39326
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.
Upvotes: 1
Reputation: 34421
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();
Upvotes: 0