Bind XML File to GridView, DataGrid, DataList or any other DataControl of ASP.NET
Tuesday, January 27, 2009 4:07:17 PM
Introduction
Sometimes, we need to show our data on DataControls like GridView, DataGrid, DataList etc. from XML data file. Checkout this demonstration, it may help you.
Basic Terms
What is XML File?
It stands for Extensible Markup Language. It is a standard that was created in order to represent data in an easily readable, self defining, and structured approach.It is a markup language that uses tags to define the structure of the data.What is XSLT File?
It stands for Extensible Stylesheet Language Transformation that has evolved from the early Extensible Stylesheet Language (XSL) standard. It specifies a language definition for XML data presentation and data transformations.XML File Creation
<?xml version="1.0" encoding="utf-8" ?>
<userinfo>
<user>
<userid>KDN101</userid>
<username>Kundan Kumar</username>
</user>
<user>
<userid>KAM101</userid>
<username>Kamal Kumar Jha</username>
</user>
<user>
<userid>SUN101</userid>
<username>Sunil Kumar Mishra</username>
</user>
<user>
<userid>RAK101</userid>
<username>Rakesh Kumar</username>
</user>
<user>
<userid>SUN102</userid>
<username>Sunny Verma</username>
</user>
</userinfo>
XSLT File Creation
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<userinfo>
<xsl:apply-templates select ="//userinfo"/>
</userinfo>
</xsl:template>
<xsl:template match ="//user">
<user>
<xsl:attribute name="UserID">
<xsl:value-of select="userid"/>
</xsl:attribute>
<xsl:attribute name="UserName">
<xsl:value-of select="username"/>
</xsl:attribute>
</user>
</xsl:template>
</xsl:stylesheet>
Steps
Step1: Create an XML file(say UserInfo.xml). This file will have the record in xml format.
Step2: Create an XSLT file.(say UserInfoTransformer.xsl) This is used to process xml data.
Step3: Drag a XML DataSource(say xmlDataSource1) and configure data source as bellow :
DataFile = ~/UserInfo.xml, Transform File= ~/UserInfoTransformer.xsl
Step4: Drag a GridView and Choose DataSouce = xmlDataSource1


Unregistered user # Monday, February 16, 2009 6:51:26 PM
Unregistered user # Sunday, February 22, 2009 1:00:13 PM