Home > DeveloperSection > Articles > Populating Grid Control, XML File and XSD File From Database

Populating Grid Control, XML File and XSD File From Database

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

Populating Grid Control, XML File and XSD File From Database

In this article, I am going to teach you, how to populate grid view control using a XML File as Data Source.

For using Customer.xml as data source, please refer my article “Populating Data in XML and Grid”.



  1. I have used WriteXml( ) and WriteXMLSchema( ) to write the data content to my XML File and to write the data content to my XSD File respectively.
  2. Server.MapPath( ) is used to get the exact location of the corresponding file.


<%@ 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>Program Illustrating To Populate Data In Gridcontrol and In XML</title>



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


        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="true"

            GridLines="None" onpageindexchanging="GridView1_PageIndexChanging">

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

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

            <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" />









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.Sql;

using System.Data.SqlClient;



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


    SqlConnection conn = new SqlConnection("Data Source=CTS;uid=sa;pwd=home;Initial Catalog=Northwind");


    protected void Page_Load(object sender, EventArgs e)


        if (!Page.IsPostBack)






    public void loadMyGridAndXML()



        SqlDataAdapter sda = new SqlDataAdapter("select * from customers",conn);

        DataSet data = new DataSet();

        sda.Fill(data, "customers");


  data.WriteXmlSchema(Server.MapPath("Customers.xsd"));            GridView1.DataSource = data;




    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)


        GridView1.PageIndex = e.NewPageIndex;







When you run the above PopulateDataInXML.aspx page, what you get is:-

1. GridView gets populated J

2. Customers.xml and Customers.xsd will be auto – coded since you use WriteXml() and WriteXmlSchema() functions. J

Your XML Output Looks Like This:-

Customers.xml :-

<?xml version="1.0"standalone="yes"?>




    <CompanyName>Alfreds Futterkiste</CompanyName>

    <ContactName>Maria Anders</ContactName>

    <ContactTitle>Sales Representative</ContactTitle>

    <Address>Obere Str. 57</Address>









    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>

    <ContactName>Ana Trujillo</ContactName>


    <Address>Avda. de la Constitución 2222</Address>

    <City>México D.F.</City>



    <Phone>(5) 555-4729</Phone>

    <Fax>(5) 555-3745</Fax>





// Since Customer table contains, 91 records,  similar to the above it appears. Each and every record will be placed inside a new <customers></customers> tag.



    <CompanyName>Wolski  Zajazd</CompanyName>

    <ContactName>Zbyszek Piestrzeniewicz</ContactName>


    <Address>ul. Filtrowa 68</Address>




    <Phone>(26) 642-7012</Phone>

    <Fax>(26) 642-7012</Fax>






<?xml version="1.0"standalone="yes"?>

<xs:schema id="NewDataSet"xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">


      <xs:choice minOccurs="0" maxOccurs="unbounded">

        <xs:element name="customers">



              <xs:element name="CustomerID" type="xs:string" minOccurs="0" />

              <xs:element name="CompanyName" type="xs:string" minOccurs="0" />

              <xs:element name="ContactName" type="xs:string" minOccurs="0" />

              <xs:element name="ContactTitle" type="xs:string" minOccurs="0" />

              <xs:element name="Address" type="xs:string" minOccurs="0" />

              <xs:element name="City" type="xs:string" minOccurs="0" />

              <xs:element name="Region" type="xs:string" minOccurs="0" />

              <xs:element name="PostalCode" type="xs:string" minOccurs="0" />

              <xs:element name="Country" type="xs:string" minOccurs="0" />

              <xs:element name="Phone" type="xs:string" minOccurs="0" />

              <xs:element name="Fax" type="xs:string" minOccurs="0" />









Design View Of Customers.xsd:

Populating Grid Control, XML File and XSD File From Database

And Your Grid looks like :-


Populating Grid Control, XML File and XSD File From Database

Happy Coding.Explore more and more as possible.

To Download this article source code,click the below download link. For your convinience, I have attached Northwind Database script with this zip file.

Server1:   http://www.4shared.com/file/W6rwJ9Ck/Populating_Grid_Control_XML_Fi.html

Server2:   http://www.mediafire.com/?glp8vnhcq012ig3


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

Follow MindStick