首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ASP.NET中返回XML?

如何在ASP.NET中返回XML?
EN

Stack Overflow用户
提问于 2009-02-12 21:19:21
回答 8查看 113.7K关注 0票数 64

对于在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()
  • Using带有正面文件内脏的aspx使用

最后,假设您需要像这样编写一个helper函数的内容:

代码语言:javascript
复制
///<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中引起警告):

代码语言:javascript
复制
<%@ Page Language="C#"
      AutoEventWireup="true"
      CodeFile="GetTheXml.aspx.cs"
      Inherits="GetTheXml" %>

接下来,我们使用Page_Load事件来写入输出:

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
   String xml = "<foo>Hello, world!</foo>";

   Response.Write(xml);
}

我们是否需要将文本ContentType更改为“/xml”?即:

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
   String xml = "<foo>Hello, world!</foo>";

   Response.ContentType = "text/xml";
   Response.Write(xml);
}

我们需要先调用Response.Clear吗?

代码语言:javascript
复制
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打电话吗?即:

代码语言:javascript
复制
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

代码语言:javascript
复制
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中指定字符集

代码语言:javascript
复制
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更好吗?是不是更糟?支持前者吗?是后者吗?

我实际上并不想写出一个字符串;我想写出一个XmlDocumentSomeone suggests I can use the XmlWriter

代码语言:javascript
复制
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对象的内容写入字符串,然后再将其写入,因为这会浪费内存。也就是说,任何这些都是不好的:

代码语言:javascript
复制
Response.Write(doc.ToString());
Response.Write(doc.InnerXml);
xmlWrite.WriteString(doc.ToString());
xmlWrite.WriteString(doc.InnerXml);

相似问题

How to return XML in ASP.NET

参考

How Return XML From ASPX in ASP.NET 1.1

Writing XML output to an ASP.NET webpage

How do you output XML from ASP.NET?

Creating an ASHX handler in ASP.NET

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/543319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档