In this article I will explain how to read the XML nodes from an XML file and then bind it to GridView control in ASP.Net using C# and VB.Net using C# and VB.Net.
 
XML File
I have an XML file named as Customers.xml with records shown as below
<Customers>
 <Customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria </ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Boise</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
 </Customer>
 <Customer>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <ContactName>Ana Trujillo</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>Avda. de la Constitución 2222</Address>
    <City>México D.F.</City>
    <PostalCode>05021</PostalCode>
    <Country>Mexico</Country>
    <Phone>(5) 555-4729</Phone>
    <Fax>(5) 555-3745</Fax>
 </Customer>
</Customers>
 
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView control with four columns that will be populated from the XML file. I have also specified OnPageIndexChanging event of the GridView control, so that if the XML file has large number of records we can view the data page wise.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Id" ItemStyle-Width="80" />
        <asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Read XML nodes and bind to GridView in ASP.Net
In the below 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.
Finally the DataSet with the XML file data is then bound to the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/Customers.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
 
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}
 
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Using ds As New DataSet()
        ds.ReadXml(Server.MapPath("~/Customers.xml"))
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Using
End Sub
 
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    GridView1.PageIndex = e.NewPageIndex
    Me.BindGrid()
End Sub
 
Read XML nodes and bind to GridView in ASP.Net
 
 
Demo
 
Downloads