Populate Grid Control From XML Document Easily

Posted by  mohan kumar
on    November-29-2011 15:25 PM

ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 4103  View(s)
Rate this:

Populate Grid Control From XML Document Easily

In this tutorial, I am going to teach you,how to populate the data grid control from Your XML Document as data source.

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>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="dgBooks" runat="server" CellPadding="4" ForeColor="#333333"

            GridLines="None">

            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <EditRowStyle BackColor="#999999" />

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 

Default.aspx.cs

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

 

public partial class _Default : System.Web.UI.Page

{

  

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            loadMyGridFromXML();

        }

    }

 

    public void loadMyGridFromXML()

    {

       DataSet myDataSet = new DataSet();

       myDataSet.ReadXml(Server.MapPath("books.xml"));

       dgBooks.DataSource = myDataSet;

       dgBooks.DataBind();

    }

}

 

Before adding the above code, create an XML Page and add the following code and save it.

Books.xml

<?xml version="1.0"encoding="utf-8" ?>

<books>

  <book>

    <title>Teach Yourself Active Server Pages 3.0 in 21 Days</title>

    <author>Mitchell</author>

    <year>1999</year>

  </book>

  <book>

    <title>Designing Active Server Pages</title>

    <author>Mitchell</author>

    <year>2000</year>

  </book>

  <book>

    <title>ASP.NET: Tips, Tutorials, and Code</title>

    <author>Mitchell</author>

    <year>2001</year>

  </book>

  <book>

    <title>ASP Unleashed</title>

    <author>Walther</author>

    <year>1998</year>

  </book>

  <book>

    <title>ASP.NET Unleashed</title>

    <author>Walther</author>

    <year>2002</year>

  </book>

  <book>

    <title>Creating Data Driven ASP.NET Applications</title>

    <author>Seven</author>

    <year>2002</year>

  </book>

</books>

 

And Your Output Looks like:-

Populate Grid Control From XML Document Easily

Happy Coding. Keep on coding as far as possible.
















Recent Activities

jayprakash sharma Created New Discussion Extracting texts from html tags   yesterday

Anchal Kesharwani added new Blog How to Use JSON   yesterday

Sumit Kesarwani added new Article Custom Error Page in Asp.Net Mvc 4   yesterday


Top Contributors

Don't want to miss updates? Please click the below button!