我有一个xml字符串,需要将字符串下载到.xml文件中。我正在开发一个asp.net Web应用程序。 以下是我的代码。
protected void btnDownloadXML_Click(object sender, EventArgs e)
{
try
{
string xmltext = divLogResults.InnerText;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmltext);
doc.Save("myfilename.xml");
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/xml";
response.AddHeader("Content-Disposition", "attachment; filename=" + doc.Name + ";");
response.Flush();
response.End();
}
catch(Exception ex)
{
throw ex;
}
}
但我只是在下载时获得一个名为#document.xml的空xml文本。我做错了什么。
答案 0 :(得分:1)
认为我混淆了代码。以下代码执行了我想要的操作。
HttpResponse response = HttpContext.Current.Response;
string xmlString = divLogResults.InnerText;
string fileName = "ExportedForm.xml";
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.Write(xmlString);