DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Restructure XML with Transformation file in ASP.NET

Shows how to restructure XML with Transformation files in ASP.NET

XML files can be structured differently. Some express properties of elements as attributes, whereas others express them as elements with inner text.
When working with the forementioned type of XML file (properties expressed as attributes), we are unable to access and display these attributes with such controls as the GridView. However, Visual Studio will enable us to transform the file so we can use it better.
This tutorial will show how we can create a Transformation file and use it to transform the XML file, so we can use all elements as originally intended.

An example XML file with properties expressed as attributes:

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

<?xml version="1.0" standalone="yes"?>
<movies>
<movie id="001">
<title>Pirates of Penzance</title>
<price>12.95</price>
<comments>
<:userComment rating="4">Best translation I've seen.</userComment>
<userComment rating="2">I like other versions better.</userComment>
</comments>
</movie>
<movie id="999">
<title>Being John Malkovich</title>
<price>24.95</price>
<comments>
<userComment rating="4">Very good.</userComment>
<userComment rating="5">Excellent.</userComment>
</comments>
</movie>
<movie id="002">
<title>The Mist</title>
<price>24.95</price>
<comments>
<userComment rating="4">Pretty good.</userComment>
</comments>
</movie>
<movie id="003">
<title>How NOT To Cook</title>
<price>23.95</price>
<comments>
<userComment rating="3">Not bad.</userComment>
</comments>
</movie>
<movie id="007">
<title>James Bond; Casino Royale</title>
<price>29.95</price>
</movie>
</movies>

We can use this as an XmlDataSource as normal, but in Visual Studio where it asks for Transformation file, we can creat a text file with the .xsl extension and point to it from the Configure Data Source window..
The XSL file will look something like this:

<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>
<xsl:strip-space elements="*"/>
<xsl:output method="xml"
omit-xml-declaration="yes"
indent="yes"
standalone="yes" />

<xsl:template match="/">
<xsl:for-each select="movies">
<xsl:element name="movies">
<xsl:for-each select="movie">
<xsl:element name="movie">
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
<xsl:attribute name="price">
<xsl:value-of select="price"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The ASPX file shows the difference in using the transformation file with a GridView control:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!


<form id="form1" runat="server">
<div>
With Transformation File:
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Movies.xml" TransformFile="~/App_Data/Movies.xsl"></asp:XmlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<br />
<br />
Without Transformation File: <asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/App_Data/Movies.xml"></asp:XmlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
</Columns>
</asp:GridView>
</form>



Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!