asp.net 模仿下载

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; using System.Xml; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Random random = new Random(); int rnumber = random.Next(1, 100); string outPutName = DateTime.Now.ToString("yyyyMMddhhmmss") + rnumber.ToString(); DataTable dt = new DataTable("Table1"); dt.Columns.Add("id", typeof(Int16)); dt.Columns.Add("name", typeof(String)); dt.Columns.Add("address", typeof(String)); dt.Columns.Add("city", typeof(String)); dt.Rows.Add(new object[] { 1, "张三", "闵行", "剑川" }); dt.Rows.Add(new object[] { 2, "李四", "徐汇", "东川" }); dt.Rows.Add(new object[] { 3, "王五", "闵行", "卢恒" }); //XmlDocument xmlDoc = new XmlDocument(); StringBuilder xmlData = new StringBuilder(); xmlData.AppendLine(@"<?xml version='1.0' encoding='gb2312' ?>"); xmlData.AppendLine(@"<root>"); foreach (DataRow dr in dt.Rows) { xmlData.AppendLine(@"<person id='" + dr["id"] + "'>"); xmlData.AppendLine(@"<name>" + dr["Name"] + "</name>"); xmlData.AppendLine(@"<address>" + dr["address"] + "</address>"); xmlData.AppendLine(@"<city>" + dr["city"] + "</city>"); xmlData.AppendLine(@"</person>"); } xmlData.AppendLine(@"</root>"); //xmlDoc.LoadXml(xmlData.ToString()); //xmlDoc.Save(savePath + outPutName);//保存路径 Response.Write(xmlData.ToString()); HttpResponse resp; resp = Page.Response; resp.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); resp.ContentType = "text/xml"; resp.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(outPutName, System.Text.Encoding.GetEncoding("gb2312")) + ".xml"); Response.End(); //this.EnableViewState = false; } }  

你可能感兴趣的:(object,String,Random,asp.net,button,encoding)