.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

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

Posted By:Karthikeyan Anbarasan       Posted Date: January 06, 2011    Points: 15    Category: LINQ    URL: http://www.f5debug.net/  

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

            }
        }
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend