articles

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

Populating Grid Control, XML File and XSD File From Database

mohan kumar8122 29-Nov-2011

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”.

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( ) 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 gets populated J

2. 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 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="NewDataSetmsdata:IsDataSet="truemsdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0maxOccurs="unbounded">
        <xs:element name="customers">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CustomerIDtype="xs:stringminOccurs="0" />
              <xs:element name="CompanyNametype="xs:stringminOccurs="0" />
              <xs:element name="ContactNametype="xs:stringminOccurs="0" />
              <xs:element name="ContactTitletype="xs:stringminOccurs="0" />
              <xs:element name="Addresstype="xs:stringminOccurs="0" />
              <xs:element name="Citytype="xs:stringminOccurs="0" />
              <xs:element name="Regiontype="xs:stringminOccurs="0" />
              <xs:element name="PostalCodetype="xs:stringminOccurs="0" />
              <xs:element name="Countrytype="xs:stringminOccurs="0" />
              <xs:element name="Phonetype="xs:stringminOccurs="0" />
              <xs:element name="Faxtype="xs:stringminOccurs="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

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

 


Updated 04-Mar-2020
Having around 5 Years experience in .NET domain.

Leave Comment

Comments

Liked By