I have this XML that I want to have in order to pull out Order #, Item (s), Qty, ItemPrice (Principal, Tax, there may also be others). Here is the XML (see below). I'm having issues with ItemPrice. In this you can use 0 for many price components, tax, principal, etc. How can I pull them onto one pin of the line?
Now I can get orderNumber, ItemNumber, Qty ... but when I pull out the price, I get a row for taxes and prinicpal.
Another question, what happens if in some cases the tax does not exist? I will get a null link, right? I want to try to handle them and just replace 0. All suggestions are very helpful.
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>
</SettlementReport>
My code is:
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 amznitemprc in amznitems.Elements("ItemPrice").Elements("Component")
select new
{
OrderNumber = (string)amznorders.Element("AmazonOrderID"),
ItemNumber = (string)amznitems.Element("AmazonOrderItemCode"),
QTY = (string)amznitems.Element("Quantity"),
PriceAmount = (string)amznitemprc.Element("Amount")
};
foreach (var order in orders)
{
Console.WriteLine("Order: {0} ItemNumber: {1} QTY: {2} PriceAmount: {3} ", order.OrderNumber, order.ItemNumber, order.QTY, order.PriceAmount);
}