Sunday, February 5, 2012

GridView XMLDataSource Example
GridView XMLDataSource Example

In this post i'm going to explain how to use XML file or XML data as XMLDataSource to populate GridView in Asp.Net 2.0,3.5,4.0.

For this example i have created a simple xml file and added it in App_Data Folder of Asp.Net application.


The data in XML file look like shown below.

01<!--?xml version="1.0" encoding="utf-8" ?-->
02<employees>
03  <details>
04  <firstname>Amit</firstname>
05  <lastname>Jain</lastname>
06  <location>Mumbai</location>
07  </details>
08  <details>
09    <firstname>user</firstname>
10    <lastname>1</lastname>
11    <location>Delhi</location>
12  </details>
13  <details>
14    <firstname>User</firstname>
15    <lastname>2</lastname>
16    <location>Noida</location>
17  </details>
18  <details>
19    <firstname>User</firstname>
20    <lastname>3</lastname>
21    <location>Bangalore</location>
22  </details>
23</employees>

First of All Add a GridView on aspx page and click on smart tag in design view of page and select new data source.
Browse to xml file path and click on ok.

XMLDataSource

When we click on ok we get error as displayed in the image.

XMLDataSource Error

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

This error is caused because the XML data is not in the format gridview can read.

GridView needs XML data in below mentioned format.

1<employees>
2  <employee firstname="Amit" lastname="Jain" location="Mumbai">
3  <employee firstname="User" lastname="2" location="Delhi">
4</employee></employee></employees>

To fix this error we need to provide XSLT Schema. Right click on solution explorer and select add new item, from new dialog box that opens, select XSLT file.

Now we need to provide XML template in this XSLT file which should look like mentioned below.

01<!--?xml version="1.0" encoding="utf-8"?-->
02<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
03    <xsl:output method="xml" indent="yes">
04 
05    <xsl:template match="/">
06      <details>
07 
08            <xsl:apply-templates select="//Details">
09      </xsl:apply-templates></details>
10 
11    </xsl:template>
12  <xsl:template match="//Details">
13    <details>
14      <xsl:attribute name="FirstName">
15        <xsl:value-of select="FirstName">
16      </xsl:value-of></xsl:attribute>
17      <xsl:attribute name="LastName">
18        <xsl:value-of select="LastName">
19      </xsl:value-of></xsl:attribute>
20      <xsl:attribute name="Location">
21        <xsl:value-of select="Location">
22      </xsl:value-of></xsl:attribute>
23    </details>
24  </xsl:template>
25</xsl:output></xsl:stylesheet>

HTML Source of GridView
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="XmlDataSource1">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
</Columns>
</asp:GridView>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
                 DataFile="~/App_Data/XMLFile.xml" 
                 TransformFile="~/App_Data/XSLTFile.xslt">
</asp:XmlDataSource>

Build and run the application.

0 comments:

Post a Comment