• XmlTextWriter输出xml到页面的问题


    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()也不能缺少。
     
  • 相关阅读:
    谈谈WPF中的CollectionView与CollectionViewSource (1)
    XPath语法备忘
    WPF中,如何将绑定源设置到单件实例
    避免让WPF资源字典变得杂乱臃肿
    自定义WPF面板
    [WPF疑难] 继承自定义窗口
    WPF高手:站出来,Show出来
    Windows Presentation Foundation Tools and Controls
    放送Ifttt邀请四枚
    使用Amazon S3 Service时报403错误的解决方法
  • 原文地址:https://www.cnblogs.com/worldreason/p/1255164.html
Copyright © 2020-2023  润新知