Wednesday, 29 January 2014

Rread the data from an XML file and bind it to GridView control in ASP.Net

For reading the data from XML file and display through the gridview , we first create the xml file name Sample.xml the We will create an ASP.NET page which reads the contents of this XML file and then displays it to the user.

Creation of XML File Name Sample.xml

 Copy and paste the contents below in a new text file and then save that file as "sample.xml" in the 
/App_Data sub-folder of your ASP.NET web application.

Placing the XML file in /App_Data sub-folder ensures that no one will be able to directly access this file from the web. It will only be accessed by our ASP.NET page which will read and display its contents.

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer>
    <CustomerID>Rakesh Kumar</CustomerID>
    <CompanyName>eCentric Solution Pvt. Ltd.</CompanyName>
    <ContactTitle>Sr. Programmer</ContactTitle>
    <Address>New Delhi</Address>
    <City>New Delhi</City>
    <PostalCode>110001</PostalCode>
    <Country>India</Country>
   </Customer>
  <Customer>
    <CustomerID>Tanishq Kumar</CustomerID>
    <CompanyName>eCentric Solution Pvt. Ltd.</CompanyName>
    <ContactTitle>Programmer</ContactTitle>
    <Address>New Delhi</Address>
    <City>New Delhi</City>
    <PostalCode>110001</PostalCode>
    <Country>India</Country>
  </Customer>
</Customers>

Reading the Contents of XML File data and then binding it to GridView control

First the data from the XML file is read into a DataSet using its ReadXml method, to which absolute path of the XML file is supplied as parameter.
Finally the DataSet with the XML file data is then bound to the GridView control
DataSet ds = new DataSet()
ds.ReadXml(Server.MapPath("~/App_Data/sample.xml"));

We need to import  following namespaces

using System.Data;
In this example we have created a page Default2.aspx which has GridView control  will be populated from the XML file. The code of aspx and cs file are given below.

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Reading an XML File</title>
    <style type="text/css">
body { font-family: Verdana; font-size: 9pt; }
.name { background-color: #F7F7F7; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
    </div>
    </form>
</body>
</html>

Default2.aspx.cs

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

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(Server.MapPath("~/App_Data/sample.xml"));
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
}

Result will be :- 

CustomerID CompanyName ContactTitle Address City PostalCode Country
Rakesh Kumar eCentric Solution Pvt. Ltd. Sr. Programmer New Delhi New Delhi 110001 India
Tanishq Kumar eCentric Solution Pvt. Ltd. Programmer New Delhi New Delhi 110001 India

:)

No comments:

Post a Comment