---
title: "Populating Grid Control, XML File and XSD File From Database"  
description: "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, p"  
author: "mohan kumar"  
published: 2011-11-29  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/826/populating-grid-control-xml-file-and-xsd-file-from-database  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Populating Grid Control, XML File and XSD File From Database

In this article, I am going to teach you, how to populate [grid view](https://www.mindstick.com/articles/53/data-grid-view) control using a [XML File](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) as [Data Source](https://www.mindstick.com/forum/160659/how-do-you-create-a-custom-filter-to-filter-any-text-in-the-data-source-in-angularjs).\

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

##### Procedure:-

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](https://www.mindstick.com/forum/2132/server-mappath-in-asp-dot-net)( ) is used to get the exact location of the corresponding file.

##### PopulateDataInXML.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>Program Illustrating To
Populate Data In Gridcontrol and In XML</title></head><body>    <form id="form1" runat="server">    <div>        <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" />        </asp:GridView>    </div>    </form></body></html>
```

PopulateDataInXML.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.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)        {            loadMyGridAndXML();        }    }     public void loadMyGridAndXML()    {        conn.Open();        SqlDataAdapter sda= new SqlDataAdapter("select * from customers",conn);        DataSet data = new DataSet();        sda.Fill(data, "customers");        data.WriteXml(Server.MapPath("Customers.xml"));  data.WriteXmlSchema(Server.MapPath("Customers.xsd"));       
    GridView1.DataSource = data;        GridView1.DataBind();        conn.Close();    }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)    {        GridView1.PageIndex
= e.NewPageIndex;        loadMyGridAndXML();    }}
```

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

1. [GridView](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) gets populated J

2. [Customers](https://www.mindstick.com/articles/44795/easy-ways-to-reach-more-customers).xml and Customers.xsd will be auto – coded since you use WriteXml() and WriteXmlSchema() functions.

##### Your XML Output Looks Like This:-

##### Customers.xml :-

```
<?xml version="1.0"standalone="yes"?><NewDataSet>  <customers>    <CustomerID>ALFKI</CustomerID>    <CompanyName>Alfreds Futterkiste</CompanyName>    <ContactName>Maria Anders</ContactName>    <ContactTitle>Sales Representative</ContactTitle>    <Address>Obere Str. 57</Address>    <City>Berlin</City>    <PostalCode>12209</PostalCode>    <Country>Germany</Country>    <Phone>030-0074321</Phone>    <Fax>030-0076545</Fax>  </customers>  <customers>    <CustomerID>ANATR</CustomerID>    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>    <ContactName>Ana Trujillo</ContactName>    <ContactTitle>Owner</ContactTitle>    <Address>Avda. de la Constitución 2222</Address>    <City>México D.F.</City>    <PostalCode>05021</PostalCode>    <Country>Mexico</Country>    <Phone>(5) 555-4729</Phone>    <Fax>(5) 555-3745</Fax>  </customers>
```

// Since Customer table contains, 91 records, similar to the above it appears. Each and every record will be [placed inside](https://www.mindstick.com/forum/12672/binding-all-college-names-to-a-college-dropdownlist-based-on-data-selected-in-another-city-dropdownlist-both-dropdownlists-are-placed-inside-the-datalist) a new <customers></customers> tag.

```
    <CustomerID>WOLZA</CustomerID>    <CompanyName>Wolski  Zajazd</CompanyName>    <ContactName>Zbyszek Piestrzeniewicz</ContactName>    <ContactTitle>Owner</ContactTitle>    <Address>ul. Filtrowa 68</Address>    <City>Warszawa</City>    <PostalCode>01-012</PostalCode>    <Country>Poland</Country>    <Phone>(26) 642-7012</Phone>    <Fax>(26) 642-7012</Fax>  </customers></NewDataSet> 
```

Customers.xsd

\

```
<?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:complexType>      <xs:choice minOccurs="0" maxOccurs="unbounded">        <xs:element name="customers">    
     <xs:complexType>            <xs:sequence>              <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" />            </xs:sequence>          </xs:complexType>        </xs:element>      </xs:choice>    </xs:complexType>  </xs:element></xs:schema>
```

##### Design View Of Customers.xsd:

![Populating Grid Control, XML File and XSD File From Database](https://www.mindstick.com/mindstickarticle/112c98e0-103f-4a68-8be5-07ac2dcadc5f/images/92c69b91-88f3-4495-9aeb-0fd9f3c6b5ee.png)

##### And Your Grid looks like :-

![Populating Grid Control, XML File and XSD File From Database](https://www.mindstick.com/mindstickarticle/112c98e0-103f-4a68-8be5-07ac2dcadc5f/images/8ca7eb4c-f3c6-4c33-96ad-47858c4dc740.png)

Happy Coding.Explore more and more as possible.

To Download this article [source code](https://www.mindstick.com/forum/23271/is-there-a-way-to-get-the-source-code-from-an-apk-file),click the below [download link](https://answers.mindstick.com/qa/95147/i-received-a-pdf-file-via-whatsapp-i-want-to-know-from-where-it-was-downloaded-can-i-get-the-download-link-from-the-pdf-file). For your convinience, I have attached Northwind [Database](https://www.mindstick.com/articles/71/how-to-create-xml-file-of-database-in-c-sharp) script with this zip file.

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

**Server2:** http://www.mediafire.com/?glp8vnhcq012ig3

---

Original Source: https://www.mindstick.com/articles/826/populating-grid-control-xml-file-and-xsd-file-from-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
