1

在 C# 中,我试图从大数据集中获取 xml 字符串:

private string GetXmlFromDecomposedPortfolio(string dataSetName, DecomposedPortfolio ptf)
{
     StringWriter writer = new StringWriter();

     System.Data.DataSet ds = new System.Data.DataSet(dataSetName);

     ds.Tables.Add(ptf.Security.Copy());   
     ds.WriteXml((TextWriter)writer, XmlWriteMode.IgnoreSchema);

     return writer.ToString();
}

但我有一个例外:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.     
    at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)    
    at System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength)    
    at System.Text.StringBuilder.Append(Char value)     at System.IO.StringWriter.Write(Char value)  
    at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)   
    at System.Data.DataTextWriter.WriteStartElement(String prefix, String localName, String ns)  
    at System.Data.XmlDataTreeWriter.XmlDataRowWriter(DataRow row, String encodedTableName)  
    at System.Data.XmlDataTreeWriter.Save(XmlWriter xw, Boolean writeSchema)    
    at System.Data.DataSet.WriteXml(XmlWriter writer, XmlWriteMode mode)   
    at System.Data.DataSet.WriteXml(TextWriter writer, XmlWriteMode mode)    
    at Decompose.Library.Render.GetXmlFromDecomposedPortfolio(String dataSetName, DecomposedPortfolio ptf)   
    at Decompose.Library.Render.SavePE()  
    at Decompose.Library.WorkFlow.ProcessBatch()

有什么建议吗?

4

1 回答 1

1

首先,您创建巨大的(显然)xml 数据

ds.WriteXml((TextWriter)writer, XmlWriteMode.IgnoreSchema);

将其克隆到string对象中后writer.ToString();,您需要的内存几乎翻了一番。

可以做的是创建XMLrow-per-row,因此创建 kind of XmlDataSetRowEnumerator,它检索XML-per-rowyield returns 结果XML

于 2012-12-12T16:17:42.750 回答