Wednesday, March 28, 2012

Reading an xml file and binding it to the Grid view using

Author : Prakash Pradeep Gopu
In this post I am going to explain how to convert the XML file it to the Dataset and howto bind the dataset of xml file to the Gridview :
Let us consider the Xml file is as follows and I want to read this xml file and need to bind to the Grid:
  <TITLE>Empire Burlesque</TITLE>
  <ARTIST>Bob Dylan</ARTIST>
  <TITLE>1999 Grammy Nominees</TITLE>
  <TITLE>For the good times</TITLE>
  <ARTIST>Kenny Rogers</ARTIST>
  <COMPANY>Mucik Master</COMPANY>
  <TITLE>Big Willie style</TITLE>
  <ARTIST>Will Smith</ARTIST>
  <TITLE>Tupelo Honey</TITLE>
  <ARTIST>Van Morrison</ARTIST>
  <ARTIST>Jorn Hoel</ARTIST>

For this we need to create one aspx page with the one grid view.Create a Aspx page like this :

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:GridView ID="grvXmlBind" runat="server">

Copy the following code in the code behind :

protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)


        protected void BindGridviewData()
          //path of the xml file
            string txml1 = @"C:\Users\pgopu\Desktop\test.xml";
            //Creating Dataset object
            DataSet ds = new DataSet();
                //reading the xml file in to dataset
                //grid view binding with data set
                grvXmlBind.DataSource = ds;

            catch (Exception ex)

 Understanding the code :

The simple method ReadXml() of dataset object will read the an xml file and it converted in to dataset.

OutPut :

No comments:

Post a Comment