c# - xml写入错误 : "error on line 3 at column 1: Extra content at the end of the document"

标签 c# asp.net xml

这里是我用来响应xml数据的c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class xmlData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.ContentType = "text/xml"; 
        String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+
                     "<note>"+
                         "<to>Tove</to>"+
                         "<from>Jani</from>"+
                         "<heading>Reminder</heading>"+
                         "<body>Don't forget me this weekend!</body>"+
                      "</note>";

        Response.Write(xml);

    }
}

但是我收到了这个错误.. 为什么?

This page contains the following errors:

error on line 3 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

最佳答案

您需要调用Response.End阻止页面的其余部分在您的 XML 之后呈现 - 或者最好不要首先将其作为“页面”。毕竟它真的不是一个页面——它只是一些 XML。听起来您真的想要一个“处理程序”(ASHX 文件和一个实现 IHttpHandler 的类),它不会自动为您添加任何内容。

关于c# - xml写入错误 : "error on line 3 at column 1: Extra content at the end of the document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9173002/

相关文章:

c# - 移动没有标题栏的表格

c# - 如何使用 C# 的正则表达式匹配行的开头或结尾?

c# - 如何将数据项添加到 List<Struct>

c# - 从表中选择数据并将其显示在带有复选框的标签中

asp.net - MS Exchange powershell命令创建了错误的电子邮件ID

java - 尝试使用简单的框架解析没有父节点的xml列表

c# - Task.Run 中的异步 lambda 与常规 lambda

c# - Asp.net mvc 5 自定义登录无需脚手架,先数据库

python - 将父标记内的 XML 节点值与作为列表中元素的元组序列进行比较

android - 如何在 Android 中创建静态(仅使用 xml 文件)ExpandableListView