Srikanth Technologies

Converting XML Elements To Attributes To Use With GridView

When we have to use an XML document with GridView through XMLDataSource, the document must have data in the form of attributes. Assume we have a document, books.xml (shown below), which contains details of books. The document contains a book element for each book with an attribute isbn and three child elements - title, author and price
<?xml version="1.0" encoding="utf-8" standalone ="yes" ?>
  <book isbn="1">
    <title>Beginning XML</title>
    <author>David Hunter</author>
  <book isbn="2">
    <title>ASP.NET 3.5 Unleashed</title>
    <author>Stephen Walther</author>
  <book isbn="3">
    <title>Programming C#</title>
    <author>Jesse Liberty</author>

But we cannot use this document with XMLDataSource as-it-is to display data through GridView. This is because GridView expects data to come in the form of  attributes and not elements.  So, we have to convert the data that is in the form of elements to attributes using XSL (EXtensible Stylesheet Language)  and XSLT (XSL transformations). The following .XSL file (convert.xsl) is used to convert book element, which contains one attribute and three child elements, to four attributes.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="">
  <xsl:output version="1.0" method="xml" indent="yes" />
  <xsl:template match="books">
    <xsl:element name="books">
  <xsl:template match="book">
    <xsl:element name="book">
      <!-- place isbn attribute first -->
      <xsl:attribute name="isbn">
        <xsl:value-of select="@isbn"/>
      <!--  convert elements to attributes -->
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
      </xsl:for-each >

XSL is used to specify how to transform XML document to HTML, XML or text.  In convert.xsl, we specify
the output to be xml. A template is used to convert book element in source to book element in output, attribute isbn to attribute isbn, and child elements of book element to attributes. XSL element  for-each is used to take one element at a time and to place it as attribute in the output. Function  name() is used to take name of the current element and  value is taken using "." with  value-of element.

If you are new to XSL, click here to learn more about XSL from a wonderful tutorial at

Now, create an page and place GridView and XMLDataSource. Specify books.xml as the XML file and convert.xsl as Transform file for XMLDataSource. Bind XMLDataSource to GridView to show data in the document as a table.

Following is the code for .aspx file.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">

<html xmlns="" >
<head runat="server">

   <form id="form1" runat="server">
    <h2>Books </h2>
    <asp:gridview ID="Gridview1" runat="server" 

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
        DataFile="books.xml" TransformFile="~/convert.xsl">     </asp:XmlDataSource>

TransformFile attribute of XmlDataSource specifies the XSL file to be used to convert the document. Another useful attribute of XmlDataSource is XPath, which allows us to specify the condition to filter content.

Click here to learn more about XPATH.