Note that there are some explanatory texts on larger screens.

plurals
  1. POStill having issues using LINQ to Query my XML
    text
    copied!<p>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?</p> <p>XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;SettlementReport&gt; &lt;Order&gt; &lt;AmazonOrderID&gt;105-6982537-6258888&lt;/AmazonOrderID&gt; &lt;ShipmentID&gt;MyShipmentIDTest1234&lt;/ShipmentID&gt; &lt;MarketplaceName&gt;Amazon.com&lt;/MarketplaceName&gt; &lt;Fulfillment&gt; &lt;MerchantFulfillmentID&gt;MyTestFulFillID12345&lt;/MerchantFulfillmentID&gt; &lt;PostedDate&gt;2008-12-15T19:33:04+00:00&lt;/PostedDate&gt; &lt;Item&gt; &lt;AmazonOrderItemCode&gt;13350774331938&lt;/AmazonOrderItemCode&gt; &lt;SKU&gt;U1409&lt;/SKU&gt; &lt;Quantity&gt;1&lt;/Quantity&gt; &lt;ItemPrice&gt; &lt;Component&gt; &lt;Type&gt;Principal&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.15&lt;/Amount&gt; &lt;/Component&gt; &lt;Component&gt; &lt;Type&gt;Tax&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.02&lt;/Amount&gt; &lt;/Component&gt; &lt;/ItemPrice&gt; &lt;/Item&gt; &lt;Item&gt; &lt;AmazonOrderItemCode&gt;13350774331939&lt;/AmazonOrderItemCode&gt; &lt;SKU&gt;U14010&lt;/SKU&gt; &lt;Quantity&gt;2&lt;/Quantity&gt; &lt;ItemPrice&gt; &lt;Component&gt; &lt;Type&gt;Principal&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.30&lt;/Amount&gt; &lt;/Component&gt; &lt;Component&gt; &lt;Type&gt;Tax&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.04&lt;/Amount&gt; &lt;/Component&gt; &lt;/ItemPrice&gt; &lt;/Item&gt; &lt;/Fulfillment&gt; &lt;/Order&gt; &lt;Order&gt; &lt;AmazonOrderID&gt;105-6982537-6259999&lt;/AmazonOrderID&gt; &lt;ShipmentID&gt;MyShipmentIDTest1234&lt;/ShipmentID&gt; &lt;MarketplaceName&gt;Amazon.com&lt;/MarketplaceName&gt; &lt;Fulfillment&gt; &lt;MerchantFulfillmentID&gt;MyTestFulFillID12345&lt;/MerchantFulfillmentID&gt; &lt;PostedDate&gt;2008-12-15T19:33:04+00:00&lt;/PostedDate&gt; &lt;Item&gt; &lt;AmazonOrderItemCode&gt;13350774331940&lt;/AmazonOrderItemCode&gt; &lt;SKU&gt;U1409&lt;/SKU&gt; &lt;Quantity&gt;1&lt;/Quantity&gt; &lt;ItemPrice&gt; &lt;Component&gt; &lt;Type&gt;Principal&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.40&lt;/Amount&gt; &lt;/Component&gt; &lt;Component&gt; &lt;Type&gt;Tax&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.15&lt;/Amount&gt; &lt;/Component&gt; &lt;/ItemPrice&gt; &lt;/Item&gt; &lt;Item&gt; &lt;AmazonOrderItemCode&gt;13350774331941&lt;/AmazonOrderItemCode&gt; &lt;SKU&gt;U14010&lt;/SKU&gt; &lt;Quantity&gt;2&lt;/Quantity&gt; &lt;ItemPrice&gt; &lt;Component&gt; &lt;Type&gt;Principal&lt;/Type&gt; &lt;Amount currency="USD"&gt;0.50&lt;/Amount&gt; &lt;/Component&gt; &lt;/ItemPrice&gt; &lt;/Item&gt; &lt;/Fulfillment&gt; &lt;/Order&gt; </code></pre> <p></p> <p><strong>MY CODE:</strong>:</p> <pre><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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload