我有这个功能生成一个XML文件..问题是,当我下载生成的文件,我有我的XML格式正确,但在本月底也将HTML页面的源代码,我已经点击了按钮来生成这个。问题是什么?这里的源代码:XmlTextWriter的C#打印也页面的HTML源代码
protected void lnkEsporta_Click(object sender, EventArgs e)
{
List<Eventi> lista_eventi = //load list
string filename = "~/data/eventi.xml";
XmlTextWriter tw = new XmlTextWriter(Server.MapPath(filename), Encoding.GetEncoding("ISO-8859-1"));
tw.Flush();
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("wrapper");
tw.WriteElementString("project", GetDao().Get<Progetti, Int32>(lista_eventi[0].idprogetto).nome);
tw.WriteStartElement("events");
foreach (Eventi evento in lista_eventi)
{
tw.WriteStartElement("event");
tw.WriteElementString("idEv", evento.idev.ToString());
tw.WriteElementString("data", evento.DataInizio.ToString());
tw.WriteElementString("sede", evento.sede.ToString());
tw.WriteElementString("location", evento.location.ToString());
tw.WriteElementString("video", evento.video ? "True" : "False");
tw.WriteElementString("tutors", evento.tutors.ToString());
tw.WriteElementString("technicians", evento.technicians.ToString());
tw.WriteStartElement("aule");
List<Aule> lista_aule = //load list
foreach (Aule aula in lista_aule)
{
tw.WriteElementString("aula", aula.descrizione.ToString(), aula.idaula.ToString());
}
tw.WriteEndElement();
tw.WriteEndElement(); //end of source event
}
tw.WriteEndElement();//end of source events
tw.WriteEndElement();//end of source wrapper
tw.WriteEndDocument();//end of source tag
tw.Close();
Response.ClearContent();
//Response.End();
Response.Clear();
Response.StatusCode = 200;
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.AddHeader("Content-Transfer-Encoding", "binary");
//Response.AddHeader("Content-Length", _Buffer.Length.ToString());
Response.ContentType = "application-download";
Response.TransmitFile(Server.MapPath(filename));
}
我已经在开始我的xml,之后它开始与页面源代码...我能做什么?非常感谢您的帮助!
有一点需要注意:'XmlWriter'是建立在内存中的XML文档,然后写出来比较痛苦。除非你真的期待大文件,否则我只会使用LINQ to XML。 –