Our article regarding, how to load XML data into GridView in ASP.NET using C#; Read data from an XML file and bind it to GridView control in ASP.NET and it will be very useful for beginners to learn more about GridView. For this we are creating XML file and insert the data into XML file.
In previous articles we explained DataBinding a DetailsView, Creating and Consuming Web Service, ASP.NET Validation Controls, Cross-Page Posting, Create Captcha Image and Binding DropDownList with Database. Now we move for the load XML data into GridView in ASP.NET.
We are creating simple XML file with the student details. Let move on that step-by-step manners. For this we are creating ASP.NET Web Application in Visual Studio 2010 using C#.
Following are the steps for load XML data into GridView in ASP.NET using C#.
Create and Add XML File
Create XML file as below and save as “StudentDetails.xml”
<?xml version="1.0" encoding="utf-16"?> <StudentDetails> <StudentDetail RollNumber="1" Name="Name1" Email="Email1@gmail.com" ContactNumber="111111" /> <StudentDetail RollNumber="2" Name="Name2" Email="Email2@gmail.com" ContactNumber="222222" /> <StudentDetail RollNumber="3" Name="Name3" Email="Email3@gmail.com" ContactNumber="333333" /> <StudentDetail RollNumber="4" Name="Name4" Email="Email4@gmail.com" ContactNumber="444444" /> <StudentDetail RollNumber="5" Name="Name5" Email="Email5@gmail.com" ContactNumber="555555" /> <StudentDetail RollNumber="6" Name="Name6" Email="Email6@gmail.com" ContactNumber="666666" /> <StudentDetail RollNumber="7" Name="Name7" Email="Email7@gmail.com" ContactNumber="777777" /> <StudentDetail RollNumber="1" Name="Name1" Email="Email1@gmail.com" ContactNumber="111111" /> <StudentDetail RollNumber="2" Name="Name2" Email="Email2@gmail.com" ContactNumber="222222" /> <StudentDetail RollNumber="3" Name="Name3" Email="Email3@gmail.com" ContactNumber="333333" /> <StudentDetail RollNumber="4" Name="Name4" Email="Email4@gmail.com" ContactNumber="444444" /> <StudentDetail RollNumber="5" Name="Name5" Email="Email5@gmail.com" ContactNumber="555555" /> <StudentDetail RollNumber="6" Name="Name6" Email="Email6@gmail.com" ContactNumber="666666" /> <StudentDetail RollNumber="7" Name="Name7" Email="Email7@gmail.com" ContactNumber="777777" /> <StudentDetail RollNumber="1" Name="Name1" Email="Email1@gmail.com" ContactNumber="111111" /> <StudentDetail RollNumber="2" Name="Name2" Email="Email2@gmail.com" ContactNumber="222222" /> <StudentDetail RollNumber="3" Name="Name3" Email="Email3@gmail.com" ContactNumber="333333" /> <StudentDetail RollNumber="4" Name="Name4" Email="Email4@gmail.com" ContactNumber="444444" /> <StudentDetail RollNumber="5" Name="Name5" Email="Email5@gmail.com" ContactNumber="555555" /> <StudentDetail RollNumber="6" Name="Name6" Email="Email6@gmail.com" ContactNumber="666666" /> <StudentDetail RollNumber="7" Name="Name7" Email="Email7@gmail.com" ContactNumber="777777" /> </StudentDetails>
Add XML file under XML folder

Add XML
Add HTML Markup
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="5"> <Columns> <asp:BoundField DataField="RollNumber" HeaderText="RollNumber" ItemStyle-Width="80" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Email" HeaderText="Email" ItemStyle-Width="150" /> <asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber" ItemStyle-Width="150" /> </Columns> </asp:GridView>
Add Namespace
using System.Data;
Add Code-Behind Source
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.BindStudentDetails(); } } private void BindStudentDetails() { using (DataSet ds = new DataSet()) { ds.ReadXml(Server.MapPath("~/XML/StudentDetails.xml")); Gridview1.DataSource = ds; Gridview1.DataBind(); } } protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e) { Gridview1.PageIndex = e.NewPageIndex; this.BindStudentDetails(); }
In this code, 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 and the DataSet with the XML file data is then bound to the GridView control.
OnPageIndexChanging event is use for GridView paging.
Run Project and Check Output

Output
Source Code
help@codescratcher.com
The post Load XML Data into GridView in ASP.NET appeared first on Code Scratcher.