XSLT in .NET

by Svelmoe 1. June 2009 13:39

I have been working quite a bit with doing XSLT on some large XML files lately.
During this I worked with the XslCompiledTransform class in .NET, which make transformations really easy and it works very fast.

An advantage of this class is also that it enables you to utilize scripts within your XSLT file, which enables you to make for example C# functions, for various tasks. I’ll write more about that in a later blog entry to keep this one simple and to the point.

The syntax (in vb.net) for doing a transformation using XslCompiledTransform is as simple as the following:

    'Create settings which eanble scripting in the XSLT file
    
Dim xmlSettings As New 
XsltSettings
    xmlSettings.EnableScript 
= True
    
'create a XML reader for the XLST file
    
Dim xsltFile As XmlReader XmlReader.Create("XSLT FILE"
)
    
'create a Transform object
    
Dim xslTransform As New 
XslCompiledTransform
    
'Load XSLT file with Settings into transform object
    
xslTransform.Load(xsltFilexmlSettingsNothing
)
    
'Do the transformation.
    
xslTransform.Transform("INPUT XML FILE""OUTPUT XML FILE")
  

The main disadvantage I’ve found for using this transformation is the huge memory usage it uses.
In my own situation, the process used about 3.5 times as much memory as my source XML file, meaning that a source file about 450MB my .NET process was killed on a 32 bit computer with an out of memory exception.

The actual memory usage will of course vary depending on how complex your transformation is, so the 3.5 number is not meant to be nothing more than an indicator that it is memory intensive.
It is however very fast, and I managed to transform very large files in a manner of seconds, thus if you can live with the memory consumption it is quite effective.

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

About Svelmoe

My real name is Allan Svelmøe Hansen.

I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004. Svelmoe.dk is a place for my every day thoughts and reactions and the occasional technical blog entry.

I also blog about SQL and MS SQL Server but have moved those posts to www.sqlstuff.dk so in case you are looking for more about that, please visit that website.