您的位置:首页 > 其它

XmlTextWriter输出xml到页面的问题

2008-07-29 00:28 471 查看
protected void Page_Load(object sender, EventArgs e)

{

Response.ContentType = "text/xml";

this.Response.Clear();

XmlTextWriter xtw = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xtw.Formatting = Formatting.Indented;

xtw.Indentation = 3;

xtw.WriteStartDocument(true);

xtw.WriteStartElement("data");

xtw.WriteAttributeString("dd", "xx");

xtw.WriteEndElement();

xtw.WriteEndDocument();

Response.End();

}

输出的结果不对。最后从网上看到少了两句:

protected void Page_Load(object sender, EventArgs e)

{

Response.ContentType = "text/xml";

this.Response.Clear();

XmlTextWriter xtw = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xtw.Formatting = Formatting.Indented;

xtw.Indentation = 3;

xtw.WriteStartDocument(true);

xtw.WriteStartElement("data");

xtw.WriteAttributeString("dd", "xx");

xtw.WriteEndElement();

xtw.WriteEndDocument();
xtw.Flush();

xtw.Close();

Response.End();

}

经过测试, Flush和Close方法只要有其一就可以正常。最好应该是两个都要。真是郁闷,xmltextwriter竟然不能自己提交缓存。
另外需要注意Response.End()也不能缺少。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: