How to Bind XML file to Gridview in Asp.net


Introduction: In this post I will explain how we can bind the XML file to Gridview in Asp.net.
Description:
I have created a XML file named Student_Detail.xml and have data below mention:
<?xml version="1.0" encoding="utf-8" ?>
<Detail>
  <Student>
  <Name>Vijay</Name>
  <Address>Himachal</Address>
  <Marks>600</Marks>
  </Student>
  <Student>
    <Name>Anil</Name>
    <Address>Delhi</Address>
    <Marks>800</Marks>
  </Student>
  <Student>
    <Name>John</Name>
    <Address>Kerla</Address>
    <Marks>900</Marks>
  </Student
</Detail>

Now add a new webform to project. Drag and drop the
Gridview Data control from Toolbox>Data control.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
      <Columns>
                <asp:TemplateField HeaderText="STUDENT NAME">
                                       <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="STUDENT ADDRESS">
                                       <ItemTemplate>
                        <asp:Label ID="lbladdress" runat="server" Text='<%# Eval("ADDRESS") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MARKS OBTAINED">
                                       <ItemTemplate>
                        <asp:Label ID="lblclass" runat="server" Text='<%# Eval("Marks") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                           </Columns>
    </asp:GridView>

Now to go .aspx.cs page and add namespace.
using System.Xml;
using System.Data;

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindxml();
        }
    }
    private void Bindxml()
    {
        DataSet dt = new DataSet();
        dt.ReadXml(Server.MapPath("Student_Detail.xml"));
        if (dt.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        else
        {
            Response.Write("No Record Found");
        }
    }

In VB

Imports System.Data
Imports System.Xml

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bindxml()
        End If
    End Sub
    Private Sub Bindxml()
        Dim dt As New DataSet()
        dt.ReadXml(Server.MapPath("Student_Detail.xml"))
        If dt.Tables(0).Rows.Count > 0 Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        Else
            Response.Write("No Record Found")
        End If
    End Sub

Run the project and Check the result.

Related Articles on Gridview:
Ø  How to use RadioButtonList control inside the Gridview inAsp.net                         
Ø  How to Bind Gridview with Datareader in asp.net

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget