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; } } }

 

 

posted on 2013-07-10 10:36  王洪洪  阅读(2231)  评论(0编辑  收藏  举报