【发布时间】:2010-10-07 07:08:55
【问题描述】:
我遇到了许多在 ASP.NET 中返回 XML 的半解决方案。不过,我不想盲目地复制和粘贴一些在大多数情况下都能正常工作的代码;我想要 正确 代码,并且我想知道 为什么 它是正确的。我要批评;我想要信息;我要知识;我想要理解。
以下是代码片段,按照复杂度递增的顺序,代表我看到的一些部分解决方案,包括每个问题引起的一些进一步的问题,我想在这里回答。
一个彻底的答案必须说明为什么我们必须拥有或不能拥有以下任何东西,或者解释为什么它不相关。
- Response.Clear();
- Response.ContentType = "text/xml";
- Response.ContentEncoding = Encoding.UTF8;
- Response.ContentEncoding = Encoding.UTF16;
- Response.ContentType = "text/xml; charset=utf-8";
- Response.ContentType = "text/xml; charset=utf-16";
- Response.End()
- 使用去掉前端文件内容的 aspx
- 使用 ashx 文件
最后,假设你需要像这样编写一个辅助函数的内容:
///<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 更改为 "text/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 之后还可能发生什么需要我们立即结束响应?
text/xml 的 content-type 是否足够,或者应该改为 text/xml; charset=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();
}
或者应该是不吗?在内容类型中有一个字符集,但没有设置属性,会搞砸服务器吗?
为什么不用其他内容类型,例如:
- UTF-8
- utf-16
- UTF-16
应该在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。这个好吗?坏的?更好的?更差?快点?慢点?内存更密集?占用内存少?
我read你应该渲染
页面的 Render() 方法中的 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
【问题讨论】:
-
-1 有没有人向你解释:如何总结一个问题?并且,请问一些令人高兴的事情,而不是“某事”......
-
那里,balexandre,它现在是一个社区 wiki。随时提供帮助。
-
几乎不可能理解你的问题究竟是什么。
-
嗯,我明白了。我在这里是因为我在问同样的问题,除了我确实想编写一个 XML 字符串的小例外。对围绕这个主题的歧义进行了很好的总结!
-
从不使用
text/xml,没有这样的哑剧!提供 XML 时,您必须使用 mimeapplication/xml并具有 XML 命名空间:<div xmlns="http://www.w3.org/1999/xhtml"><p>Example.</p></div>。