Library tutorials & articles

XML transformations in .NET - Part I

Presenting the data

Rendering this raw data on a webpages requires an XSLT template that converts or transforms the xml into HTML. Below is a sample xsl that will process the “computers.xml” file.

computers.xsl

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="computers">
<h2>Computer Shopper</h2>

<b>Computyer Type:</b> <xsl:value-of select="computer/@type"/><br/>
<b>PC Specs:</b> <br/>
<b>CPU:</b> <xsl:value-of select="computer/processor"/><br/>
<b>Ram:</b> <xsl:value-of select="computer/ram"/><br/>
<p />

<b>Price: </b> <xsl:value-ofselect="computer/price/@currencySymbol"/>
<xsl:value-of select="computer/price"/>
</xsl:template>
</xsl:stylesheet>

Here are the results of the transformation:

.NET Transformation Objects:

So how do you tell an XSL to process an xml file for transformation? There are a few ways. This article focuses on the built in .NET Transformer class. First familiarize yourself with the Code-behind below:

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents ltl_htmlResults As System.Web.UI.WebControls.Literal

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim XMLDoc As XPathDocument
        Dim XSLTDoc As XslTransform
        'Prepare StringWriter for results
        Dim sw As StringWriter = New StringWriter()
        'Load xml file into XMLDoc
        XMLDoc = New XPathDocument(Server.MapPath("computers.xml"))
        'Load XSL
        XSLTDoc = New XslTransform()
        XSLTDoc.Load(Server.MapPath("computers.xsl"))
        'Transform XMLDoc and dump HTML results to stringwriter -> sw
        XSLTDoc.Transform(XMLDoc, Nothing, sw)
        'Pull results out of stringwriter and populate literal
        ltl_htmlResults.Text = sw.ToString()
    End Sub

Comments

  1. 19 Sep 2006 at 19:55

    well how to get the reverse of it , i mean what if we have a web page that gets updated & then the RSS(XML) of it has to be out , how can asp.net perform such function...

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of XML transformations in .NET - Part I.

Leave a comment

Sign in or Join us (it's free).

 JLogic

Related podcasts

  • Episode 10: LINQ

    K Scott leads us in a discussion of LINQ, including: What is it How introducing LINQ to .NET changed the framework LINQ Providers LINQ to XML LINQ to SQL - how it's different from EF, tips and tricks, when to use it Links: LINQpad 3rd Party LIN...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!