繁体   English   中英

提供XML文件供asp.net c#中的大量记录下载

[英]Provide XML file for download with large number of records in asp.net c#

我需要提供xml文件以便下载6,00,000条记录(数据可能会增加)但不允许将文件保存在磁盘上。 我直接将xml写入流然后提供下载时遇到问题,所以我首先在磁盘上创建了xml文件并将数据写入其中并尝试读取它并提供下载然后从磁盘中删除文件。 但是在“Byte [] b = File.ReadAllBytes(filepath);”中获取“system.outofmemoryexception”。

以下是我的代码:

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

还有其他方法可以处理大量记录吗?

您可以直接将XmlWriter连接到Response OutputStream,而无需中间文件。 通过将BufferOutput设置为false可以在发送到客户端之前阻止完整的repsonse缓冲服务器端。

请注意,多个Replace语句将导致额外的内存压力/更多字符串被GC编辑。

string name = string.Format("attachment; filename={0:yyyyMMddHHmmss}.xml", DateTime.Now);
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

HttpContext.Current.Response.BufferOutput = false; //start streaming immideately

HttpContext.Current.Response.AppendHeader("Content-Disposition", name);

DataTable dataTable = dsData.Tables[0];             

XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true };

using (XmlWriter xmlWriter = XmlWriter.Create(
         HttpContext.Current.Response.OutputStream,  // The OutputStream of the HttpResponse
         xws))
{
  xmlWriter.WriteStartElement("root");

  var cleanedColNames = new Dictionary<DataColumn, string>();
  foreach (DataColumn dataColumn in dataTable.Columns)
  {
      cleanedColNames.Add(dataColumn, 
          dataColumn.ColumnName.Replace("\n\r", " ")
                               .Replace("\n", " ")
                               .Replace("\r", " "));
  }

  foreach (DataRow dataRow in dataTable.Rows)
  {
    xmlWriter.WriteStartElement("datanode");
    foreach (DataColumn dataColumn in dataTable.Columns)
    {
         xmlWriter.WriteElementString(
             cleanedColNames[dataColumn],  
             Convert.ToString(dataRow[dataColumn]).Replace("\n\r", " ")
                                                  .Replace("\n", " ")
                                                  .Replace("\r", " "));
                        }
         xmlWriter.WriteEndElement();
     }
     xmlWriter.WriteEndElement();
     xmlWriter.Flush();
     xmlWriter.Close();
   }

HttpContext.Current.Response.End();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM