Question

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

Was it helpful?

Solution

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

Maybe use a FileStream instead?

OTHER TIPS

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

D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top