Wednesday, 27 July 2011

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.

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

An example XML file with properties expressed as attributes:

<?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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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>

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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

<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!

No comments:

Post a Comment