LINQ to XML : How to create a XML tree by loading the XML file using C#

Create a console application:
  1. Open Visual Studio 2010 by going Start | All Programs | Microsoft Visual Studio 2010 | Right click on Microsoft Visual Studio 2010 and click on Run as administrator.
  2. Go to File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the Windows node.
  4. In the Templates pane, select Console Application.
  5.  Enter the Name as LINQ and then click OK.
  6.  In the solution explorer, right click on the solution and then click on Properties.
  7. Select the Application tab, check whether “.Net Framework 3.5” is selected for Target Framework.
  8. Select the Build tab, check whether “Any CPU” is selected for Platform Target.

Add the following References:

  1. System.Xml.Linq.dll

Add the following Namespace:

  1. Using System.Xml.Linq;

Program.cs:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

 

 

namespace Linq

{

    class Program

    {

        static void Main(string[] args)

        {

            XElement employees = XElement.Load(@"C:\ Projects\Linq\Linq\Employees.xml");                             

            Console.WriteLine(employees);

            Console.ReadLine();         

        }

    }

}