Blog

LINQ to XML : How to create a XML tree by passing the string using C#

By Vijai Anand Blogs | LINQ Mar 29, 2012
In this blog you will see how to create a XML tree by passing the string 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 LinqSamples

{

    class Program

    {

        static void Main(string[] args)

        {

            XElement employees = XElement.Parse(

     @"<EmployeeDetails>

        <Employee>

            <Name>Vijai Anand</Name>

            <Id>206</Id>

            <Designation>Associate</Designation>

            <Department>SharePoint</Department>

            <Location>Bangalore</Location>

        </Employee>      

    </EmployeeDetails>");

            Console.WriteLine(employees);

            Console.ReadLine();         

        }

    }

}

 

          

          

 

COMMENT USING