Search This Blog

Sunday, July 17, 2011

How to show XML Data in the Grid View

  • Create a Page with the Name XMLSample.aspx
  • Write the Below controls in the .aspx source Page.
  • Create an xml file on the desktop with Name Students ( See example xml at the End)
<asp:GridView ID="gvList" runat="server">
        </asp:GridView>
        Path:
        <asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />
  • Write the Below code in the Code behind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;


public partial class XMLSample : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        HttpPostedFile hpf=FileUpload1.PostedFile;
        string fName=FileUpload1.PostedFile.FileName;
        System.Data.DataSet ds = new System.Data.DataSet();
        ds.ReadXml(fName);
        gvList.DataSource = ds.Tables[0];
        gvList.DataBind();
        
        
    }
}

Note : Example Xml file.


<Students>
  <Student>
    <id>1312</id>
    <name>123</name>
    <age>123123</age>
  </Student>
  <Student>
    <id>6</id>
    <name>Ashoke</name>
    <age>23</age>
  </Student>
</Students>

No comments: