LINQ example using XML file


This article describes basic LINQ query that getting some data from XML file and bind a gridview.Here there is XML file name employees.xml where each employee have four attribute as FirstName, LastName, Department, Location. Our aim is to select all Department into a dropdownbox and then when we select one department and press Button then all Employee details will appear into a Gridview. I have taken two class file as Employee.cs and EmployeeService.cs and one aspx page

Here there is XML file name employees.xml where each employee have four attribute as FirstName, LastName, Department, Location. Our aim is to select all Department into a dropdownbox and then when we select one department and press Button then all Employee details will appear into a Gridview. I have taken two class file as Employee.cs and EmployeeService.cs and one aspx page
Employee.cs.....


public class Employee
{
public Employee()
{
//
// TODO: Add constructor logic here
//
}
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
public string Location { get; set; }

}

EmployeeService.cs.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.IO;
///
/// Summary description for EmployeeServices
///

public class EmployeeServices
{
public EmployeeServices()
{
//
// TODO: Add constructor logic here
//
}

XElement empXML = XElement.Load(@"D:\Bubi\practice linq\LINQexample\App_Data\employees.xml");
public List GetDepartments()
{
var deptquery = from emp in empXML.Descendants("employee")
group emp by emp.Element("Department").Value
into empGroup
select empGroup.First().Element("Department").Value;
return deptquery.ToList();
}

public List GetEmpByDept(string department )
{
IEnumerable empquery = from emp in empXML.Descendants("employee")
where emp.Element("Department").Value == department
select new Employee
{
Id = emp.Element("Id").Value,
Department = emp.Element("Department").Value,
FirstName = emp.Element("FirstName").Value,
LastName = emp.Element("LastName").Value,
Location = emp.Element("Location").Value
};
return empquery.ToList();
}
}

employees.xml...



<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<Id>M1</Id>
<FirstName>Mahua</FirstName>
<LastName>Shown</LastName>
<Department>IT</Department>
<Location>Taipei</Location>
</employee>
<employee>
<Id>A231</Id>
<FirstName>Ayush</FirstName>
<LastName>Shown</LastName>
<Department>IT</Department>
<Location>Taipei</Location>
</employee>
<employee>
<Id>C3</Id>
<FirstName>Indrajit</FirstName>
<LastName>Shown</LastName>
<Department>Chemistry</Department>
<Location>Linkou</Location>
</employee>
<employee>
<Id>M1</Id>
<FirstName>Partha</FirstName>
<LastName>Dutta</LastName>
<Department>Cs</Department>
<Location>Kolkata</Location>
</employee>
</employees>


Default.aspx page.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
EmployeeServices empsrv = new EmployeeServices();
GridView1.DataSource = empsrv.GetEmpByDept(DropDownList1.SelectedItem.Text);
GridView1.DataBind();
}
}

Source page of Default.aspx....

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetDepartments" TypeName="EmployeeServices">
</asp:ObjectDataSource>

</div>
</form>
</body>
</html>


Comments

Author: Phagu Mahato15 Sep 2013 Member Level: Gold   Points : 0

Code snippet for how to create an XML Document with using LINQ
YourNamespace StudentNM = "urn:lst-std:std";
YourDoc YourDoc = new YourDoc( new YourDeclaration("1.0", "UTF-16", null),
new YourEle(empNM + "Employees",
new YourEle("Employee",
new YourComment("Only 3 Eles for demo purposes"),
new YourEle("StudentId", "5"),
new YourEle("Name", "Mohini"),
new YourEle("Your sex ", "Female")
)));

StringWriter sw = new StringWriter();
YourDoc.Save(sw);
Console.WriteLine(sw);
VB.NET (Converted Code)
Dim empNM As YourNamespace = "urn:lst-emp:emp"

Dim YourDoc As New YourDoc(New YourDeclaration("1.0", "UTF-16", Nothing), New YourEle(empNM + "Students", _
New YourEle("Student", _New YourComment("Only 3 Elements"),
New YourEle("StudentId", "5"), _
New YourEle("Name", "Mohini"), _
New YourEle("Your sex", "Female"))))

Dim sw As New StringWriter()
YourDoc.Save(sw)
Console.WriteLine(sw)



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: