写入xml时出错:";第3行第1列的错误:文档末尾的额外内容”;

本文关键字:错误 额外内容 文档 1列 xml 出错 写入 quot 3行第 | 更新日期: 2023-09-27 18:23:40

这里是我用来响应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.

写入xml时出错:";第3行第1列的错误:文档末尾的额外内容”;

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