邓石的博客

成功与其说是取决于人的才能,不如说取决于人的热忱。
随笔 - 138, 文章 - 1, 评论 - 74, 引用 - 2
数据加载中……

[心得]使用ASPX输出XML应注意的问题

Ajax应用程序中服务器端如果使用ASPX返回XML格式数据,一般将VS生成的HTML字符先清除,代码文件中直接使用Response.Write输出XML字符串。
 
需要注意的是要设置Response的相应属性客户端才能正确解析字符串。
 
首先要设置ContentType 属性:
Response.ContentType = "text/xml";
如果xml字符串中包含中文则还要设置Charset属性:
Response.Charset = "GB2312";
 
然后按照XML文件的格式输出内容:
Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
 
今天就是因为这个Charset让我郁闷了好久,因为XML字符中中包含中文字符而又没有设置Charset,因此XMLHttpRequest.responseXML总是null,查了半天资料终于恍然大悟。

posted on 2006-07-08 23:42 阅读(454) 评论(3)  编辑 收藏

评论

#1楼    回复  引用    

http://www.lemongtree.com/zh-cn/item,360.html

我一般用
XmlTextWriter类
2006-07-09 15:44 | 幻想曲.Net [未注册用户]

#2楼    回复  引用    

Response.ContentType = "text/xml";
Response.Charset = "GB2312";
Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\"?>");

Response.Write("<pictures>");
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
Response.Write("<item>");
Response.Write("<name>" + row["ALBUM_NAME"].ToString() + "</name>");
Response.Write("<url>" + row["ALBUM_URL"].ToString() + "</url>");
Response.Write("<description>" + row["ALBUM_DESC"].ToString() + "</description>");
Response.Write("</item>");
}
}
Response.Write("</pictures>");
还是不行
2007-06-26 19:09 | yl [未注册用户]

#3楼    回复  引用    

responseXML还是null
2007-06-26 19:11 | yl [未注册用户]

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
博客园首页

新闻频道

社区

小组

博问

网摘

闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-08-02 15:59 编辑过
成果网帮您增加网站收入


相关链接: