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