© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
fyntore.repopyop

❔ 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);
}  
 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);
}  
d1.xml2.78KB
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Connecting to database via String in XML
C#CC# / help
2y ago
LINQ
C#CC# / help
2y ago
❔ LINQ
C#CC# / help
3y ago