龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

asp.net处理输出XML出现文本内容中发现无效字符的问题

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
直接在asp.net生成输出xml,总是出现如下问题: 无法显示 XML 页。 使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 ----------------------------------------------

直接在asp.net生成输出xml,总是出现如下问题:

无法显示 XML 页。
使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。


--------------------------------------------------------------------------------

文本内容中发现无效字符。处理资源

 

查看源代码时xml内容是没有错的,那怎么可以在浏览器中直接浏览生成显示XML数据呢?主要在输入前加入如下语句:

            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.ContentType = "application/xml";

请看我写的一个输出xml函数:

        public static void ResponseXml(string username, string result, string msgid, string code)
        {
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.ContentType = "application/xml";
           
            StringBuilder strXml = new StringBuilder();
            strXml.Append("<?xml version='1.0' ");
            strXml.Append("encoding='UTF-8'");
            strXml.Append(" ?>");
            strXml.Append("\n<SmsApp>");
            strXml.Append("\n   <username>");
            strXml.Append(username);
            strXml.Append("</username>");
            strXml.Append("\n <result><![CDATA[ ");
            strXml.Append(result);
            strXml.Append(" ]]></result>");
            strXml.Append("\n <msgid>");
            strXml.Append(msgid);
            strXml.Append("</msgid>");
            strXml.Append("\n <code>");
            strXml.Append(code);
            strXml.Append("</code>");
            strXml.Append("\n</SmsApp>");
           
            HttpContext.Current.Response.Write(strXml.ToString());
        }

精彩图集

赞助商链接