Background
There is often a need for applications to bind a Grid View from an EXtensible Markup Language (XML) file that is widely used to communicate for cross-platform applications. To learn more about XML please refer to the many articles and books on this website. Let us see the procedure for creating a XML file so students can understand it.
Step 1
Create the project as:
Step 3:

We are now done with the code, now run the application and the output will be as follows.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Give the project a name such as "BindGridfromxml" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - "Default.aspx" page.
- Drag and Drop one grid view to the Default.aspx page. Then the page will look such as follows.
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body bgcolor="silver">
- <form id="form1" runat="server">
- <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <EditRowStyle BackColor="#2461BF" />
- <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#EFF3FB" />
- <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F5F7FB" />
- <SortedAscendingHeaderStyle BackColor="#6D95E1" />
- <SortedDescendingCellStyle BackColor="#E9EBEF" />
- <SortedDescendingHeaderStyle BackColor="#4870BE" />
- </asp:GridView>
- </form>
- </body>
- </html>
Step 2
- Adding and creating XML file
After adding the XML file it wll look such as follows:
- <?xml version="1.0" encoding="utf-8" ?>
Now let us add some records to the XML file as in the following:
- <?xml version="1.0" encoding="utf-8" ?>
- <Customer>
- <Customerinfo>
- <Name>Vithal Wadje</Name>
- <city>Mumbai</city>
- <Address>Sion, Mumbai</Address>
- </Customerinfo>
- <Customerinfo>
- <Name>Sudhir Wadje</Name>
- <city>Latur</city>
- <Address>Latur</Address>
- </Customerinfo>
- <Customerinfo>
- <Name>U D</Name>
- <city>Mumbai</city>
- <Address>DN,Road Mumbai</Address>
- </Customerinfo>
- <Customerinfo>
- <Name>Anil Kumar</Name>
- <city>Delhi</city> z
- <Address>Nehru House</Address>
- </Customerinfo>
- </Customer>
In the preceding sample code the Custmorinfo tag represents the table name in the data table and Name, City and Address represents the column name. When you want to add records then you need to add a row within the Customerinfo tag every time.
Let us see the preceding XML file in the DataSet using the DataSet Visualizer as in the following:

From the preceding example it's clear that the Customerinfo tag represents the table name in the data table and that Name, City and Address represents the column name.
Step 4:
Now let us add the code into default.aspx.cs for the Page Load event to bind the Grid view as in the following:
Default.aspx.cs
- using System;
- using System.Data;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- DataSet ds = new DataSet();
- protected void Page_Load(object sender, EventArgs e)
- {
- ds.ReadXml(Server.MapPath("~/Customer.xml"));
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- }
Note
- Download the Zip file from the attachment for the full source code of the application.
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

Vithal WadjePosted Apr 27, 2015, 1:00 PM
thanks
Alok GuptaPosted Apr 27, 2015, 4:51 AM
Very good
Vithal WadjePosted Feb 10, 2015, 9:18 AM
bind as above simpe
Rajveer singhPosted Feb 10, 2015, 1:09 AM
Sir I want to bind Gridview from Xml Webservices in asp.net
Vithal WadjePosted Jan 27, 2014, 11:57 AM
thanks Abhijit sir
Abhijit PatilPosted Jan 27, 2014, 5:01 AM
nice artical