Reading XML string using LINQ

By using XDocument.Parse() method we can convert/read xml string to XDocumnet in LINQ. and we can set the xml to gridview as a result set. i.e. we can select the required columns and can assign it to a gridview.

Example:

Design:


Code Behind (C#):

XDocument xdoc = XDocument.Parse(@" Thomas Executive Accounts 5000 Wills Manager Accounts 24000 Brod Manager Finance 28000 Smith Analyst Finance 21000 "); var res = from emp in xdoc.Root.Elements() select new { EmployeeName = emp.Element("Name").Value, Department = emp.Element("Department").Value, Salary = emp.Element("Salary").Value }; gvXML.DataSource = res; gvXML.DataBind();

Output:

EmployeeName

Department

Salary

Thomas

Accounts

5000

Wills

Accounts

24000

Brod

Finance

28000

Smith

Finance

21000