2012-12-12 51 views
1

編寫XML在C#我想要得到一個大的數據集的XML字符串:OutOfMemoryException異常試圖從數據集

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() 

任何建議?

+0

[使用System.Data或System.Xml名稱中的類的受管代碼解決方案在處理大型數據集時可能會遇到System.OutOfMemoryException](http://blogs.msdn.com/b/vsofficedeveloper/archive/2008 /10/10/stringbuilder-outofmemoryexception.aspx)。 –

回答

1

首先你創造巨大的(顯然)XML數據

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

克隆後它,進入string對象writer.ToString();,讓你幾乎雙倍的內存需要。

什麼你可以做的,是創造XML行每行,所以建立一種XmlDataSetRowEnumerator,檢索XML-per-rowyield return S中產生的XML

+0

那麼將數據集分成不同的數據集呢? –

相關問題