0

I'm maintaining an old .NET 1.1 application that runs out of memory during an XSL transformation on a large data set. XML is written directly to a FileStream:

Dim xslDoc As New System.Xml.Xsl.XslTransform
Dim sourceStream As New System.IO.MemoryStream
Dim sourceStreamWriter As New System.IO.StreamWriter(sourceStream)
Try
  ' Load data and schema from DataSet
  Dim sourceDoc As New System.Xml.XmlDocument
  dataSet.WriteXml(sourceStreamWriter , XmlWriteMode.WriteSchema)
  sourceStreamWriter.Flush()
  sourceStream.Position = 0
  sourceDoc.Load(sourceStream)

  ' Load XSLT
  xslDoc.Load("c:\path\to\transform.xslt")

  ' Transform (FAILS HERE)
  xslDoc.Transform(sourceDoc, Nothing, fileStream, New System.Xml.XmlUrlResolver)
Finally
  schemaStreamWriter.Close()
  schemaStream.Close()
End Try

I have found similar questions on the matter, but they usually describe a situation where resulting XML is written to a MemoryStream rather than a FileStream, or refer to using XslCompiledTransform, which unfortunately is only available from .NET 2.0 and up.

How do I avoid using up all the memory during transformation?

(Similar (unanswered) question here: http://vbcity.com/forums/t/151286.aspx)

3
  • Does it work on a small data set? Commented Jan 8, 2014 at 18:02
  • Indeed it does. It's been running fine for years, however, the data sets have never been as large as the one they are trying to process in this particular instance. Commented Jan 8, 2014 at 18:05
  • 1
    Without the code it's impossible to say, but there are lots of ways to go wrong in XSLT. One common way to make transforms more efficient is to break them up into smaller steps to avoid duplicate traversal. You can also use keys in come cases for the same effect. Commented Jan 8, 2014 at 19:30

2 Answers 2

1

What you could try, instead of

Dim sourceStream As New System.IO.MemoryStream
Dim sourceStreamWriter As New System.IO.StreamWriter(sourceStream)
Try
  ' Load data and schema from DataSet
  Dim sourceDoc As New System.Xml.XmlDocument
  dataSet.WriteXml(sourceStreamWriter , XmlWriteMode.WriteSchema)
  sourceStreamWriter.Flush()
  sourceStream.Position = 0
  sourceDoc.Load(sourceStream)

  ' Load XSLT
  xslDoc.Load("c:\path\to\transform.xslt")

  ' Transform (FAILS HERE)
  xslDoc.Transform(sourceDoc, Nothing, fileStream, New System.Xml.XmlUrlResolver)

use

Dim dataDoc as XmlDataDocument = new XmlDataDocument(dataSet)
  ' Load XSLT
  xslDoc.Load("c:\path\to\transform.xslt")

  ' Transform (FAILS HERE)
  xslDoc.Transform(dataDoc, Nothing, fileStream, New System.Xml.XmlUrlResolver)
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. However, the XSLT expects the schema to be present in the input XML (in the original code XmlWriteMode.WriteSchema is used) which it is not when looking at the XML generated by XmlDataDocument. Is there a way to ensure that the schema is included?
@mbjdev, sorry, there is only the constructor taking the DataSet as an argument, if that does not create the contents your XSLT expects then my suggestion will not allow you to check whether it solves the memory problem. I am not sure what else you could try, in .NET 2.0 or later I would try to "get rid" of the sourceStream and sourceStreamWriter by wrapping them into Using/End Using before the Transform call is made but in .NET 1.1 I don't remember whether it is possible to dispose of those objects. Maybe someone else has an idea.
0

We found no way to solve this from within .NET. We may attempt to optimize the XSLT in the future.

(For now, the client has agreed to have the data split up into batches, resulting in multiple files)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.