Display xml file data into gridview using datatable

Satyapriyanayak
Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 3211
Data.xml file

<?xml version="1.0" standalone="yes"?>
<studentdata>
<student>
<sid>s001</sid>
<sname>Satyapriya</sname>
<saddress>Orissa</saddress>
<smarks>100</smarks>
</student>
</studentdata>

protected void btn_showdata_Click(object sender, EventArgs e)
{

string Path = Server.MapPath("Data.xml");
//student Must match with the element name in your file
DataTable dt = new DataTable("student");

//Add Columns in datatable
//Column names must match XML File nodes
dt.Columns.Add("sid", typeof(System.String));
dt.Columns.Add("sname", typeof(System.String));
dt.Columns.Add("saddress", typeof(System.String));
dt.Columns.Add("smarks", typeof(System.String));

//Read XML File And Display Data in GridView
dt.ReadXml(Path);
GridView1.DataSource = dt;
GridView1.DataBind();
}

Comments or Responses

Login to post response