Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Practical XML Usage  »  Final steps
 »  Home  »  Data Programming  »  XML  »  Practical XML Usage  »  Final steps
Practical XML Usage
by Gaidar Magdanurov | Published  08/20/2006 | .NET Newbie XML | Rating:
Final steps

Step 3. Create XSLT files for desired transformations
Talking about XSLT language is out of  scope of this article, so I just present one of XSLT files listings below.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:output method="html" />
<xsl:template match="/">
            <xsl:value-of select="//@band"/>
        <xsl:value-of select="//@band"/> - Discography (<xsl:value-of select="//@firstYear"/> - <xsl:value-of select="//@lastYear"/>)
    <xsl:for-each select="//album">
            <xsl:value-of select="./title"/> (<xsl:value-of select="./label"/>, <xsl:value-of select="./year"/>)
        <xsl:for-each select="./songs/song">
            <xsl:value-of select="."/>

Step 4. Join all parts together
Now it is time to create a simple application. I don't think it is necessary to discuss the application. If you wish to play with the sample you can download the attached archive and look though the code yourself. Below I will dwell only on the crucial points.

Validating an XML file

The easiest way to validate an XML file against the schema is to use the XmlDocument class that has the Validate method which in its turn uses a callback function to notify  found errors. If it is not necessary to get information about validation error the callback function can set some flag to show that a given file is not valid.

'''''' Class field
Private isValid As Boolean = true

'''''' The code for validation
Dim xml As New XmlDocument()
xml.Schemas.Add(Nothing, schemaPath)
xml.Validate(AddressOf ValidationHandler)
If isValid Then
    ''' XML file is valid
    ''' XML file is invalid
End If

'''''' Callback method
Private Sub ValidationHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
    isValid = False
End Sub

It is important to validate XML files before performing transformations or using deserialization to objects because invalid XML files will result in run-time errors.

Performing a transformation

To transform an XML file using XSLT presented above we will use XslCompiledTransform which resides in System.Xml.Xsl namespace.

Dim xml As New XslCompiledTransform()
xml.Transform(xmlPath, outputPath)

Serializing/Deserializing objects

If you look through the System.Xml.Serialization namespace you will find a class XmlSerializer which makes ut very easy to carry out serialization/deserialization.


Private disco As discography
Dim xml As New XmlSerializer(Type.GetType("SimpleXSLT.discography"))
Dim xmlFileStream, As New FileStream(xmlPath, FileMode.Open, FileAccess.Read)
disco = CType(xml.Deserialize(xmlFileStream), discography)


Dim disco As New discography
'''' set disco properties here

Dim xml As New XmlSerializer(Type.GetType("SimpleXSLT.discography"))
Dim xmlFileStream As New FileStream(xmlPath, FileMode.Create, FileAccess.Write)
xml.Serialize(xmlFileStream, disco)

Sponsored Links