This article will show you how you can bind xml file data to
a gridview and how you can perform search operation on xml data and display it
on gridview in asp.net and c#.net.
Some of my previous articles are as follows: Bind
Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net, Bind
Asp.net ListBox Control by XML File Data Using DataSet in C#.Net, Image
Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net,
How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application.
xml version="1.0" encoding="utf-8" ?>
<CountryModel>
<Country>
<ID>1</ID>
<COUNTRY>India</COUNTRY>
<POPULATION>20000</POPULATION>
</Country>
<Country>
<ID>2</ID>
<COUNTRY>Pakistan</COUNTRY>
<POPULATION>30000</POPULATION>
</Country>
<Country>
<ID>3</ID>
<COUNTRY>Sri Lanka</COUNTRY>
<POPULATION>40000</POPULATION>
</Country>
<Country>
<ID>4</ID>
<COUNTRY>Nepal</COUNTRY>
<POPULATION>50000</POPULATION>
</Country>
<Country>
<ID>5</ID>
<COUNTRY>Bhutan</COUNTRY>
<POPULATION>60000</POPULATION>
</Country>
</CountryModel>
|
Now come to your aspx page add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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>Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net
</title>
<link href="Styles/Site.css" rel="stylesheet"
type="text/css"
/>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td>
Search :<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" Width="100%">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
In above I have added only a gridview.
Now add generate the click event of the button and add the
below code into your .cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetData();
}
}
public
void GetData()
{
try
{
DataSet
dsResult = new DataSet();
dsResult.ReadXml(Server.MapPath("~/XML/XMLFile1.xml"));
if
(dsResult.Tables.Count != 0)
{
if
(dsResult.Tables[0].Rows.Count > 0)
{
GridView1.DataSource
= dsResult.Tables[0];
GridView1.DataBind();
}
}
}
catch
(Exception ex)
{
}
}
protected
void Button1_Click(object
sender, EventArgs e)
{
XDocument
doc = XDocument.Load(Server.MapPath("~/XML/XMLFile1.xml"));
var
records = (from data in
doc.Root.Elements("Country")
where TextBox1.Text == ""
? true : data.Element("COUNTRY").Value.ToUpper().Contains(TextBox1.Text.ToUpper())
select data);
if
(records != null)
{
DataTable
dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("COUNTRY", typeof(string));
dt.Columns.Add("POPULATION", typeof(string));
foreach
(var item in
records)
{
DataRow
dr = dt.NewRow();
dr["ID"] = (string)item.Element("ID");
dr["COUNTRY"] = (string)item.Element("COUNTRY");
dr["POPULATION"] = (string)item.Element("POPULATION");
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
}
}
}
}
|
In above code I have bind the record by reading xml file
into gridview and on search first I have read the xml file and by using linq
query I have filtered the data present in the XDocument. After that I have
added all the filtered record into a data table and then added data to the
datatable and bind it to gridview.
Now we have done run the application and check the output.
0 comments:
Please let me know your view