Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> ASP.Net & Web Forms >> Populate Grid Control From XML Document Easily
Populate Grid Control From XML Document Easily
Populate Grid Control From XML Document Easily


by mohan kumar on 11/29/2011 3:25:36 PM

Views: 4045       Comments: 0

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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by mohan kumarRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4937
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.