Load an XML and query using LINQ

This code sample shows how to load an XML and use LINQ to query and display the result

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Xml.Linq;
05using System.Text;
06 
07namespace Demo
08{
09    class Program
10    {
11        static void Main(string[] args)
12        {
13            string xmlFileName = @"C:\CustomerOrders.xml";
14   XDocument cust = XDocument.Load(xmlFileName);
15      
16   Console.WriteLine("Elements in XML:");
17   var qResult = from c in cust.Elements()
18                     select c.Name;
19   foreach (var itemList in qResult)
20   {
21       Console.WriteLine(itemList);
22   }
23   Console.Write("Press Enter to continue:");
24   Console.ReadLine();
25 
26        }
27    }
28}

Ebook Download
View all
Learn
View all