.NET 在线生成XML文档,并提供下载

今天在写自己的CMS系统时,需要生成一个网站有KEY,其中一种方法就是让客户把生成的KEY直接下载后放到网站根目录下,这样主要是方便一些内网的网站。
那怎样动态生成XML文档并提供下载呢?其他这种原理和生成EXECL一样。代码如下:
int insertID=3;
string domain=”www.w17x.com“;
string enddate=”2009-12-1″;
string Dcode=”xlxcn”;

string xml = “<key>\r\n” +
@”<webid>” + insertID.ToString() + “</webid>\r\n” +
@”<domain>” + domain + “</domain>\r\n” +
@”<enddate>” + enddate + “</enddate>\r\n” +
@”<code>” + Dcode + “</code>\r\n” +
@”</key>”;

Response.Clear();
Response.Buffer = true;
//Response.Charset = “UTF8″;
Response.AppendHeader(“Content-Disposition”, “attachment;filename=key.xml”);
Response.ContentEncoding = System.Text.Encoding.UTF8;

Response.ContentType = “application/text”;
System.IO.StringWriter ostringwriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new HtmlTextWriter(ostringwriter);

Response.Write(xml);
Response.End();

此时下载的文件就是key.xml为文件名的一个XML文件

原文地址:https://www.cnblogs.com/dzone/p/1967229.html