1

我有这个函数来生成一个 xml 文件.. 问题是当我下载生成的文件时,我的 xml 格式正确,但最后还有我点击生成按钮的页面的 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,之后它从页面源代码开始......我该怎么办?非常感谢您的帮助!

4

1 回答 1

0

我建议你失踪了Response.End()

另请参阅文件名和 mime 问题 - ASP.NET 下载文件 (C#)

    Response.ContentType = "application/xml";
    Response.AddHeader("Content-Disposition", String.Format("inline; filename={0}.xml", Path.GetFileName(Path)));

    Response.WriteFile(path); // 
    Response.Flush();
    // Response.Close(); // end should do this anyway so optional
    Response.End(); // end forces the server to send the request immediately. Failing to 
于 2013-07-29T08:35:10.180 回答