我在ASP.NET中返回XML的任务遇到了很多半解决方案.我不想盲目地复制和粘贴一些恰好在大多数情况下工作的代码; 我想要正确的代码,我想知道它为什么是正确的.我要批评; 我想要信息; 我需要知识; 我想要理解.
下面是代码片段,按照复杂性的增加顺序,代表我看到的一些部分解决方案,包括每个问题引起的一些问题,以及我想在这里回答的问题.
一个彻底的答案必须解决为什么我们必须拥有或不得拥有以下任何东西,或者解释为什么它无关紧要.
最后,想象一下你需要编写一个这样的辅助函数的内容:
///<summary>Use this call inside your (Page_Xxx) method to write the
///xml to the web client. </summary>
///<remarks>See for https://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net
///for proper usage.</remarks>
public static void ReturnXmlDocumentToWebClient(
XmlDocument document,
Page page)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我看到的每个解决方案都从一个空的aspx页面开始,并从前端文件中删除所有HTML(这会在Visual Studio中导致警告):
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="GetTheXml.aspx.cs"
Inherits="GetTheXml" %> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码将报告导出到csv:
byte[] csvBytes = AttachmentFileAgent.GetAttachmentFilesCSVBytes(context.Request["appCode"], performingPerson);
context.Response.AddHeader("content-disposition", "attachment; filename=ApplicationReport_" + context.Request["appCode"] + ".csv");
context.Response.ContentType = "text/csv;charset=utf-8";
context.Response.Charset = "utf-8";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.OutputStream.Write(csvBytes, 0, csvBytes.Length);
Run Code Online (Sandbox Code Playgroud)
但是希伯来文的内容却是胡言乱语('>''''''''''''''''').我试过各种编码(UTF8,Unicode,ASCII),但没有任何作用......