Hi Gurus,
So I am close with this, but when one of the components under my ItemPrice Element (Tax for instance) is missing then my whole Order/Line is not returned. Any thoughts on what I can do if a component like "Tax" is sometimes missing?
XML:
<?xml version="1.0" encoding="UTF-8"?>
<SettlementReport>
<Order>
<AmazonOrderID>105-6982537-6258888</AmazonOrderID>
<ShipmentID>MyShipmentIDTest1234</ShipmentID>
<MarketplaceName>Amazon.com</MarketplaceName>
<Fulfillment>
<MerchantFulfillmentID>MyTestFulFillID12345</MerchantFulfillmentID>
<PostedDate>2008-12-15T19:33:04+00:00</PostedDate>
<Item>
<AmazonOrderItemCode>13350774331938</AmazonOrderItemCode>
<SKU>U1409</SKU>
<Quantity>1</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">0.15</Amount>
</Component>
<Component>
<Type>Tax</Type>
<Amount currency="USD">0.02</Amount>
</Component>
</ItemPrice>
</Item>
<Item>
<AmazonOrderItemCode>13350774331939</AmazonOrderItemCode>
<SKU>U14010</SKU>
<Quantity>2</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">0.30</Amount>
</Component>
<Component>
<Type>Tax</Type>
<Amount currency="USD">0.04</Amount>
</Component>
</ItemPrice>
</Item>
</Fulfillment>
</Order>
<Order>
<AmazonOrderID>105-6982537-6259999</AmazonOrderID>
<ShipmentID>MyShipmentIDTest1234</ShipmentID>
<MarketplaceName>Amazon.com</MarketplaceName>
<Fulfillment>
<MerchantFulfillmentID>MyTestFulFillID12345</MerchantFulfillmentID>
<PostedDate>2008-12-15T19:33:04+00:00</PostedDate>
<Item>
<AmazonOrderItemCode>13350774331940</AmazonOrderItemCode>
<SKU>U1409</SKU>
<Quantity>1</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">0.40</Amount>
</Component>
<Component>
<Type>Tax</Type>
<Amount currency="USD">0.15</Amount>
</Component>
</ItemPrice>
</Item>
<Item>
<AmazonOrderItemCode>13350774331941</AmazonOrderItemCode>
<SKU>U14010</SKU>
<Quantity>2</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">0.50</Amount>
</Component>
</ItemPrice>
</Item>
</Fulfillment>
</Order>
MY CODE::
XDocument customer = XDocument.Load(@"C:\LinqToXML.xml");
var orders = from amznorders in customer.Root.Elements("Order")
from amznfulfill in amznorders.Elements("Fulfillment")
from amznitems in amznfulfill.Elements("Item")
from amznitemprc1 in amznitems.Elements("ItemPrice").Elements("Component")
from amznitemprc2 in amznitems.Elements("ItemPrice").Elements("Component")
let amznitemprinc = amznitemprc1.Element("Amount")
where (string)amznitemprc1.Element("Type") == "Principal"
let amznitemprtax = amznitemprc2.Element("Amount")
where (string)amznitemprc2.Element("Type") == "Tax"
select new
{
OrderNumber = (string)amznorders.Element("AmazonOrderID"),
ItemNumber = (string)amznitems.Element("AmazonOrderItemCode"),
Qty = amznitems == null ? "0" : (string)amznitems.Element("Quantity"),
PriceAmount = amznitemprinc == null ? String.Empty : (string)amznitemprinc,
TaxAmount = amznitemprtax == null ? String.Empty : (string)amznitemprtax
};
foreach (var order in orders)
{
Console.WriteLine("Order: {0} ItemNumber: {1} QTY: {2} {3} {4}", order.OrderNumber, order.ItemNumber, order.Qty,order.PriceAmount,order.TaxAmount);
}