Load an XML and query using LINQ and Navigate the data to get the result

Posted by Karthikanbarasan under LINQ category on | Points: 40 | Views : 1831
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)

"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);


Comments or Responses

Login to post response