对于在ASP.NET中返回XML的任务,我遇到了许多半途而废的解决方案,但我不想盲目地复制和粘贴一些在大多数情况下都可以工作的代码;我想要正确的代码,我想知道为什么它是正确的。我需要批评;我需要信息;我需要知识;我需要理解。
下面是代码片段,按复杂性递增的顺序,代表了我所见过的一些部分解决方案,包括每个解决方案引起的一些进一步的问题,我希望在这里得到答案。
一个完整的答案必须解决为什么我们必须有或不能有以下任何东西,或者解释为什么它是不相关的。
最后,假设您需要像这样编写一个helper函数的内容:
///<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)
{
...
}
我看到的每个解决方案都是从获取一个空的aspx页面开始,并修剪掉前面文件中的所有HTML (这会在Visual Studio中引起警告):
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="GetTheXml.aspx.cs"
Inherits="GetTheXml" %>
接下来,我们使用Page_Load
事件来写入输出:
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.Write(xml);
}
我们是否需要将文本ContentType更改为“/xml”?即:
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.ContentType = "text/xml";
Response.Write(xml);
}
我们需要先调用Response.Clear
吗?
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.Clear();
Response.ContentType = "text/xml";
Response.Write(xml);
}
我们真的需要这样做吗?难道Response.Clear
不需要在<% ... %>
之外确保前面文件中的代码为空(甚至没有空格或回车符)吗?
如果有人在代码前端文件中留下了空行或空格,Response.Clear
是否会使其更加健壮?
使用ashx是否与空白aspx主文件相同,因为可以理解它不会输出HTML?
我们需要给Response.End
打电话吗?即:
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.Clear();
Response.ContentType = "text/xml";
Response.Write(xml);
Response.End();
}
在Response.Write
之后还会发生什么,需要我们立即结束响应
字符集的内容类型是否足够,或者应该改为文本/xml;text/xml
=utf-8
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.Clear();
Response.ContentType = "text/xml; charset=utf-8";
Response.Write(xml);
Response.End();
}
或者应该明确地说是而不是?在内容类型中有一个字符集,但没有设置属性,会不会搞砸服务器?
为什么不是一些其他的内容类型,例如:
是否应在Response.ContentEncoding
中指定字符集
protected void Page_Load(object sender, EventArgs e)
{
String xml = "<foo>Hello, world!</foo>";
Response.Clear();
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
Response.Write(xml);
Response.End();
}
使用Response.ContentEncoding
比把它塞进Response.ContentType
更好吗?是不是更糟?支持前者吗?是后者吗?
我实际上并不想写出一个字符串;我想写出一个XmlDocument
。Someone suggests I can use the XmlWriter
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xml = GetXmlDocumentToShowTheUser();
Response.Clear();
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
using (TextWriter textWriter = new StreamWriter(
Response.OutputStream,
Encoding.UTF8))
{
XmlTextWriter xmlWriter = new XmlTextWriter(textWriter);
// Write XML using xmlWriter
//TODO: How to do this?
}
}
注意Response.OutputStream
的用法,而不是Response.Write
。这样好吗?坏的?好些了吗?更糟?更快?慢点?更多内存密集型?内存密集度更低?
I read,你应该渲染
在页面的
()方法中呈现XML,以避免在使用Page_Load()时遇到的分块问题。
什么是分块?分块有什么问题,使用Page_Render
如何消除这些问题?
我不希望将XmlDocument
对象的内容写入字符串,然后再将其写入,因为这会浪费内存。也就是说,任何这些都是不好的:
Response.Write(doc.ToString());
Response.Write(doc.InnerXml);
xmlWrite.WriteString(doc.ToString());
xmlWrite.WriteString(doc.InnerXml);
相似问题
参考
How Return XML From ASPX in ASP.NET 1.1
Writing XML output to an ASP.NET webpage
https://stackoverflow.com/questions/543319
复制相似问题