I need to provide xml file for download around with 6,00,000 records(data may increase) but not allowed to save file on disk. I was facing issue in directly writing xml to stream & then providing for download , so i have first created xml file on disk & writing data to it and trying to read it in byes & provide for download and then delete file from disk. But getting "system.outofmemoryexception" in "Byte[] b = File.ReadAllBytes(filepath);".
Below is my code:
string name = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
string filename = "TestFile.xml";
string filepath = ConfigurationManager.AppSettings["XmlFiles"] + "\\" + filename;
DataTable dataTable = dsData.Tables[0];
FileStream fs =new FileStream(filepath, FileMode.Create);
XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true };
using (XmlWriter xmlWriter = XmlWriter.Create(fs,xws))
{
xmlWriter.WriteStartElement("root");
foreach (DataRow dataRow in dataTable.Rows)
{
xmlWriter.WriteStartElement("datanode");
foreach (DataColumn dataColumn in dataTable.Columns)
{
xmlWriter.WriteElementString(dataColumn.ColumnName.Replace("\n\r", " ")
.Replace("\n", " ").Replace("\r", " "), Convert.ToString(dataRow[dataColumn]).Replace("\n\r", " ").Replace("\n", " ").Replace("\r", " "));
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
xmlWriter.Flush();
xmlWriter.Close();
}
fs.Close();
Byte[] b = File.ReadAllBytes(filepath);
if (File.Exists(filepath))
File.Delete(filepath);
string s = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + s + ".xml" + "");
HttpContext.Current.Response.BinaryWrite(b);
HttpContext.Current.Response.End();
Is there any other way to handle large number of records?