生成 XML 文件并以 XML 格式显示

发布于 2024-09-19 08:56:00 字数 1831 浏览 3 评论 0原文

查看了教程,但无法正常工作:

default.aspx:

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="rssPubba.Default" %>

default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        XmlDocument doc = new XmlDocument();

        // XML declaration
        XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
        doc.AppendChild(declaration);

        // Root element: article
        XmlElement root = doc.CreateElement("article");
        doc.AppendChild(root);

        // Sub-element: author
        XmlElement author = doc.CreateElement("author");
        author.InnerText = "Faisal Khan";
        root.AppendChild(author);

        // Attribute: isadmin
        XmlAttribute isadmin = doc.CreateAttribute("isadmin");
        isadmin.Value = "true";
        author.Attributes.Append(isadmin);

        // Sub-element: title
        XmlElement title = doc.CreateElement("title");
        title.InnerText = "Sample XML Document";
        root.AppendChild(title);

        // Sub-element: body (CDATA)
        XmlElement body = doc.CreateElement("body");
        XmlNode cdata = doc.CreateCDataSection("This is the body of the article.");
        body.AppendChild(cdata);
        root.AppendChild(body);

        doc.Save(Response.OutputStream);
    }

但是,当我尝试显示它时,浏览器似乎将其解释为标记:

输出:

<article> 
  <author isadmin="true">Faisal Khan</author> 
  <title>Sample XML Document</title> 
  <body><![CDATA[This is the body of the article.]]></body> 
</article>

我必须进行哪些更改?

Looked at a tutorial but couldn't get this working:

default.aspx:

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="rssPubba.Default" %>

default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        XmlDocument doc = new XmlDocument();

        // XML declaration
        XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
        doc.AppendChild(declaration);

        // Root element: article
        XmlElement root = doc.CreateElement("article");
        doc.AppendChild(root);

        // Sub-element: author
        XmlElement author = doc.CreateElement("author");
        author.InnerText = "Faisal Khan";
        root.AppendChild(author);

        // Attribute: isadmin
        XmlAttribute isadmin = doc.CreateAttribute("isadmin");
        isadmin.Value = "true";
        author.Attributes.Append(isadmin);

        // Sub-element: title
        XmlElement title = doc.CreateElement("title");
        title.InnerText = "Sample XML Document";
        root.AppendChild(title);

        // Sub-element: body (CDATA)
        XmlElement body = doc.CreateElement("body");
        XmlNode cdata = doc.CreateCDataSection("This is the body of the article.");
        body.AppendChild(cdata);
        root.AppendChild(body);

        doc.Save(Response.OutputStream);
    }

however when I try to display it the browser seem to interpreting it as markup:

output:

<article> 
  <author isadmin="true">Faisal Khan</author> 
  <title>Sample XML Document</title> 
  <body><![CDATA[This is the body of the article.]]></body> 
</article>

What changes to I have to make?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2024-09-26 08:56:22

我怀疑它正在清除到目前为止的输出并写入正常的页面数据。两个选项:

  • 使用 ashx 而不是 aspx 来创建处理程序,以便它知道您没有尝试呈现页面。如果它总是意味着生成 XML 文档,这可能是最明智的方法。
  • 写入数据后完成请求,例如通过调用 Response.CompleteRequest()

(我还建议使用 LINQ to XML 作为比旧的 DOM API 更好的 API 来构建 XML 文档,但这取决于您:)

I suspect it's clearing the output so far and writing the normal page data. Two options:

  • Use an ashx instead of aspx to create a handler, so that it knows you're not trying to render the page. That's probably the most sensible approach, if it's always meant to be generating an XML document.
  • Complete the request when you've written the data, e.g. by calling Response.CompleteRequest()

(I'd also recommend using LINQ to XML as a rather nicer API for constructing XML documents than the old DOM API, but it's up to you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文