How to read XML data based on condition and display in gridview?


In this article I am going to explain about how to read xml data based on condition and display it. This can be help your project to get particular xml records for manipulation.

Description :


I have data in the xml file likebelow

<?xml version="1.0" encoding="utf-8" ?>
<Employee>
<emp>
<eno>101</eno>
<empname>Ravindran</empname>
<sal>35000</sal>
</emp>
<emp>
<eno>105</eno>
<empname>Mike</empname>
<sal>25000</sal>
</emp>
<emp>
<eno>112</eno>
<empname>Andrew</empname>
<sal>19000</sal>
</emp>
<emp>
<eno>120</eno>
<empname>James</empname>
<sal>15000</sal>
</emp>
</Employee>

In the above XML file I want to get only above 110 employee no records and display it in the gridview.

Client side


I have placed file upload control to get xml file data and gridview to display it.

<%@ 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>Filter Xml Data and Display it in the Grid View</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="600" align="center" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" height="40">
<b>Upload XML data into Gridview based on condition</b>
</td>
</tr>
<tr>
<td height="30">
Select XML file to upload
</td>
<td height="30">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" height="40" align="center">
<asp:Button ID="Button1" runat="server" Text="Upload and load data"
onclick="Button1_Click" />
</td>
</tr>
<tr>
<td colspan="2" height="2" align="center">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Server side


In server side I load all xml data in to datatable and then filter in datatable itseld and display it in the gri dview refer below code sample

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

}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath(filename));
ExportToGrid(Server.MapPath(filename));
}
}

void ExportToGrid(string path)
{
try
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataRow[] dr;
DataTable dtfilter = new DataTable();
//Here i load all xml data in to dataset
ds.ReadXml(path);
dt = ds.Tables[0];
//Filter here above employe no ten records
dr = dt.Select("eno > 110");
dtfilter = dr.CopyToDataTable();
GridView1.DataSource = dtfilter;
GridView1.DataBind();
}
catch (Exception ex)
{

}
//Delete after read data from server path
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
}
}


Output :


Image

Source code:


Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this code snippet is helping you to know about filter xml data through DataTable and display in the gridview.


Attachments

  • ReadXml_Source (43969-75237-ReadXml-Source.rar)
  • Comments

    No responses found. Be the first to comment...


  • 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: