TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Load an XML and query using LINQ and Navigate the data to get the result
Karthikeyan Anbarasan
Apr 01, 2011
3.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Load an XML and query using LINQ and Navigate the data to get the result.
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
Next Recommended Reading
Grouping Data in XML File using LINQ-XML