❔ Querying XML via LINQ

i have attached XML file for processing, which contain data from a electronics consumer store . I need to display Purchase Orders having the following criteria 1. Billing Address must be of state "PA" 2. Quantity > 2 3. Purchase orders must be dated between 2021 and 2022 I have used the following lambda Query to fetch data but it cannot access data in Quantity element therefore query returns empty data, how do i resolve the issue? var xdoc = XDocument.Load(@"E:\nitro\d1.xml"); var z = xdoc.Descendants("PurchaseOrders").Descendants("PurchaseOrder") .Where(e => DateTime.Parse(e.Attribute("OrderDate").Value).Year >= 2021 && ((DateTime.Parse(e.Attribute("OrderDate").Value).Year <= 2022))) .Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing") .Descendants("Items").Descendants("Quantity").Where(q => Int32.Parse(q.Value) >= 2) .Select(x => x); foreach (var item in z) { Console.WriteLine(item); }
2 Replies
KidKai25
KidKai252y ago
I would suggest you break it down to understand where things might be going wrong. Also, I see Items element is a sibling of Address instead of descendant
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server