In this article, we will discuss how to load XML data into a dataset and bind to GridView control. To do this we will use ADO.NET provider’s method.
Let’s start
We will create an PatientList.xml file.
<patients>
<patient>
<patientNo>M-2011-1</patientNo>
<title>Mr.</title>
<fullName>John</fullName>
<gender>Male</gender>
<Birthday>12/14/2000</Birthday>
<SSN>******123</SSN>
<maritalStatus>Married</maritalStatus>
<referredBy>TV</referredBy>
</patient>
<patient>
<patientNo>M-2011-2</patientNo>
<title>Mr.</title>
<fullName>Aamir Hasan</fullName>
<gender>Male</gender>
<Birthday>12/14/2001</Birthday>
<SSN>******133</SSN>
<maritalStatus>Single</maritalStatus>
<referredBy>TV</referredBy>
</patient>
<patient>
<patientNo>M-2011-3</patientNo>
<title>Miss</title>
<fullName>Saba Khan</fullName>
<gender>Female</gender>
<Birthday>12/14/1950</Birthday>
<SSN>******223</SSN>
<maritalStatus>Married</maritalStatus>
<referredBy>TV</referredBy>
</patient>
</patients>
Drag and drop GridView control on the page, i have also applied formating on it as shown.
<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>
Write following code in code behind.
C#
protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(Server.MapPath("PatientList.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim ds As New System.Data.DataSet()
ds.ReadXml(Server.MapPath("PatientList.xml"))
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End Sub
Output

Download
GridView xml file.rar (1.47 kb)
See Live demo