how to bind xml to grid view using asp.net?

aswinialuri-19361
Posted by aswinialuri-19361 under ASP.NET category on | Points: 40 | Views : 1500
code in .aspx page
drag and drop single grid view
 <asp:GridView ID="GridView1" runat="server">
</asp:GridView>


Add new item in solution explorer
add xml file
code in.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Employee>
<Emp>
<Empid>1</Empid>
<EmpName>aswini</EmpName>
</Emp>
<Emp>
<Empid>2</Empid>
<EmpName>aluri</EmpName>
</Emp>
</Employee>

code in .aspx.cs page:

add name space
 using system.xml;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}

}
public void getdata()
{
XmlTextReader xtr = new XmlTextReader(Server.MapPath("XMLFile1.xml"));
DataSet dt = new DataSet();
dt.ReadXml(xtr);
xtr.Close();
if (dt.Tables.Count != 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Comments or Responses

Login to post response