views:

546

answers:

2

Question: What is the best way to transform a large XML document (>200MB) using XSL in .Net?

Background: I have a application that feeds me large data files, I cannot change the format. In the past I have been able to translate smaller data files with no issues.

Originally I was working with the XML as strings and was running out of memory very quickly. I switched my code and now I deal with MemoryStream's to read, transform using a stylesheet and then save off a copy of the output to a separate location using filestreams.

Applying of the stylesheet causes the application to consume upwards of 1gb memory and eventually crashes.

I know I could programatically process the XML using the DOM but I would really like to stick with a generic method of applying an XSL stylesheet.

Does anyone have any pointers on how I could better manage memory while processing the XSL transformation? Below is a snippet of code where i am applying the transformation:

'xmlData is a memory stream passed into a function 
        '...
    Dim strfilepath As String = appConfigSettings.FilePaths.XslFilePath & "\" & odtrow.formatterXsl

 Dim xslt As New System.Xml.Xsl.XslCompiledTransform()
 xslt.Load(strfilepath)

 Dim xmlRead As XmlReader = XmlReader.Create(xmlData)

 newStream = New MemoryStream()
 xslt.Transform(xmlRead, Nothing, newStream) 'here is where it fails
 newStream.Position = 0
        '...

C# or VB examples are fine I can work with either...This app was a hand me down so not criticism for the vb please :) -J

+1  A: 

You're using a MemoryStream, and you run out of memory. Hmmm...

Maybe use a FileStream instead?

John Saunders
I have tried with filestream but the process takes 100x long. I was trying to somehow manage the memorystream and parse the XML at the same time. I do have a working filestream example. Even with this when you apply the XSL style sheet it consumes a ton of memory, but at least it does not crash.
Jay
Going to accept this, never found another option. I'll have to suffer with the long load times.
Jay
A: 

Try and use XPathDocument instead of XMLReader. It is optimised and much faster.

D

Dipesh