我正在维护一个旧的.NET 1.1应用程序,该应用程序在对大型数据集进行XSL转换期间内存不足。 XML直接写入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
我在这个问题上找到了类似的问题,但是他们通常会描述这样一种情况:将结果XML写入MemoryStream
而不是FileStream
,或者参考使用XslCompiledTransform
,遗憾的是,只能从.NET 2.0及更高版本获得。
如何在转换过程中避免耗尽所有内存?
(类似(未回答)问题:http://vbcity.com/forums/t/151286.aspx)
答案 0 :(得分:1)
你可以尝试什么,而不是
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)
使用
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)
答案 1 :(得分:0)
我们发现无法从.NET中解决这个问题。我们可能会在将来尝试优化XSLT。
(目前,客户已同意将数据分成批次,从而产生多个文件)