Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Load an XML and query using LINQ and Navigate the data to get the result
WhatsApp
Karthikeyan Anbarasan
13y
3.7
k
0
0
25
Blog
This code loads the XML and query using LINQ and then navigate the data to get the result.
using System;
using
System
.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string xmlFileName = @"C:\CustomerOrders.xml";
XDocument cust = XDocument.Load(xmlFileName);
var queryResults = from c in cust.Customers
where c.Country == "INDIA"
select new
{
ID = c.CustomerID,
Name = c.CompanyName,
City = c.City,
State = c.Region,
Orders = c.Orders
};
foreach (var itemQuery in queryResults)
{
Console.WriteLine(
"Customer: {0} {1}, {2}\n{3} orders:\tOrder ID\tOrder Date",
itemQuery.Name, itemQuery.City, itemQuery.State, itemQuery.Orders.Count
);
foreach (Order ood in itemQuery.Orders)
{
Console.WriteLine("\t\t{0}\t{1}", ood.OrderID, ood.OrderDate);
}
}
}
}
}
Load an XML and query using LINQ and Navigate the data to get the result
Up Next
Grouping Data in XML File using LINQ-XML
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Membership not found