connectseekers.com

Wednesday, 8 October 2014

Bind & Search XML File Data and Display in GridView in Asp.net Using C#.Net

10/08/2014 - By Pranav Singh 0

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.

So for this article first we will create a new asp.net application and create an xml file as shown below.

  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.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top