【问题标题】:How can I show an xml file in my asp.net project?如何在我的 asp.net 项目中显示 xml 文件?
【发布时间】:2012-04-04 10:57:37
【问题描述】:

在 .Net 网络服务中,当您单击“调用”按钮时,会打开一个 XML 文件。

我有一个带有按钮的页面。我希望当我单击它时打开一个 XML 页面,就像 Web 服务页面一样。我编写了以下代码,但它不返回 XML 页面。我该怎么做?

1) 我在“http://mysite/default.aspx”

2) 点击我的按钮

3) 使用 .Xml 扩展名打开此页面:“http://mysite/result.xml”

 Response.ClearHeaders();
 //Response.AppendHeader("content-disposition", "attachment;filename=result.xml");
 Response.AddHeader("content-type", "text/xml");
 Response.Write("aaaa");
 Response.End();

【问题讨论】:

    标签: c# asp.net xml response


    【解决方案1】:

    它编写了代码Response.Redirect("xmlfile.xml");,它按照你的预期显示了xml。

    【讨论】:

      【解决方案2】:

      您需要在响应中添加内容类型。

      Response.Clear();
      Response.ContentType = "text/xml";          
      Response.Write("<?xml version=\"1.0\">");
      //whatever you want
      Response.End();
      

      PS.content-disposition 将为您提供下载文件的“选项”(浏览器将下载文件,而不是将其发布到浏览器本身);

      Response.ContentType = "application/" + System.IO.Path.GetExtension(pathToFile).Substring(1).ToLower();
      Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(pathToFile)));
      Response.WriteFile(pathToFile);
      Response.End();
      

      【讨论】:

      • 您的示例代码没有打开包含我的文件内容的新窗口。
      • @breceivemail 当然不是。打开一个新窗口进行下载是第二个代码示例。但是在新窗口中打开 XML 文件取决于您(例如,将目标添加到您的锚点)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 2019-11-24
      相关资源
      最近更新 更多