gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:28 27 4
gpt4 key购买 nike

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

下面是我的代码:

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 连接到响应 OutputStream无需中间文件。通过设置 BufferOutputfalse 可以防止完整的响应在发送到客户端之前在服务器端进行缓冲。

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

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

关于c# - 在asp.net c#中提供大量记录的XML文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24690554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com