生成 XML 文件并以 XML 格式显示
查看了教程,但无法正常工作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑它正在清除到目前为止的输出并写入正常的页面数据。两个选项:
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:
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 :)