My Opera is closing 3rd of March

The IT World

.NET World

Bind XML File to GridView, DataGrid, DataList or any other DataControl of ASP.NET

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

That's all....Keep Coding..Bye Bye..

GridView's Check Box handling on Client Side Using Java Script in ASP.NETDeleting duplicate records from table in SQL Server

Comments

Unregistered user Monday, February 16, 2009 6:51:26 PM

Arun writes: Thanks, Its really nice and fully working example.

Unregistered user Sunday, February 22, 2009 1:00:13 PM

Ved Prakash writes: Hi Sir, really a good article. http://vedprakashsays.blogspot.com

Write a comment

New comments have been disabled for this post.

February 2014
M T W T F S S
January 2014March 2014
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28