XmlWriter 表示一个编写器,该编写器提供一种快速、非缓存和只进的方式来生成包含 XML 数据的流或文件。这个就可以不占用内存,将数据放入磁盘中。也就不会出现内存溢出

public class FileExercise : System.Web.Services.WebService { [WebMethod] public void HelloWorld() { // 生成临时文件 string tFName = System.IO.Path.GetTempFileName();//@"C:\ResponseStream.txt" System.IO.FileStream stream = System.IO.File.Open(tFName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); XmlWriter writer = XmlWriter.Create(stream); writer.WriteStartDocument(); writer.WriteRaw("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"); writer.WriteRaw("<soap:Body>"); writer.WriteStartElement(MethodInfo.GetCurrentMethod().Name + "Response", @"http://tempuri.org/"); writer.Flush(); // 返回的主体 writer.WriteString("Hello word!"); // 将内存缓冲数据写进磁盘 ,当数据量大的时候分批处理并及时写入磁盘,可以降低内存压力 writer.Flush(); writer.WriteEndElement(); writer.WriteRaw("</soap:Body>"); writer.WriteRaw("</soap:Envelope>"); writer.WriteEndDocument(); writer.Flush(); writer.Close(); stream.Close(); HttpContext context = HttpContext.Current; //context.Response.ContentType = "application/soap+xml; charset=utf-8"; context.Response.WriteFile(tFName); return; } } }

 

 

相关文章:

  • 2021-08-25
  • 2022-02-27
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2021-05-20
  • 2021-12-08
猜你喜欢
  • 2022-12-23
  • 2021-12-05
  • 2022-01-05
  • 2021-06-07
  • 2021-04-10
  • 2021-07-01
  • 2022-01-22
相关资源
相似解决方案