zoukankan      html  css  js  c++  java
  • 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()也不能缺少。
     
  • 相关阅读:
    【车】平安车险费用
    【光环国际】掌握项目经理12个工作流程
    【光环国际】500强公司7个项目管理工具
    数据仓库和商业智能演进五个阶段
    【技术与商业案例解读笔记】095:Google大数据三驾马车笔记
    15道最常考的SpringBoot面试题,你都遇到过哪些?
    用户画像4:标签数据开发
    plsql 传参
    shell专题(二):Shell解析器
    shell专题(三):Shell脚本入门
  • 原文地址:https://www.cnblogs.com/worldreason/p/1255164.html
Copyright © 2011-2022 走看看