从stringbuilder中读取xml,里面有中文,怎么办?

wyljz 2010-11-18 01:58:10
我的stringbuilder中保存的是已经拼接好的xml数据文件,准备在界面中用dataset读取,使用dataset的readxml方法读取时,由于数据中有中文,一直出现错误提示:给定编码中的字符无效
stringbuilder中的内容:
<?xml version="1.0" standalone="yes"?>
<dsArticle>
<dtArticle>
<id>3069</id>
<title>0dfewtrewter</title>
<content>sdfsdfg</content>
<createTime>2010-11-18 13:24:48</createTime>
</dtArticle>

<dtArticle>
<id>45</id>
<title>中文标题</title>
<content>中文内容 </content>
<createTime>2010-11-17 8:33:44</createTime>
</dtArticle>
<dtArticle>
<id>3062</id>
<title>475.test</title>
<content>ttttttttrrrf</content>
<createTime>2010-11-17 8:44:50</createTime>
</dtArticle>

</dsArticle>

读取的代码:
DataSet ds = new DataSet();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
ds.ReadXml(ms);

出错了
...全文
157 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiuchunYoung 2010-11-18
  • 打赏
  • 举报
回复

//再通过查询解析出来的的字符串有没有GB2312 的字段,来判断是否是GB2312格式的,如果是,则重新以GB2312的格式解析
Regex reGB = new Regex("GB2312", RegexOptions.IgnoreCase);
Match mcGB = reGB.Match(str);
if (mcGB.Success)
{
StreamReader reader2 = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
str = reader2.ReadToEnd();
}
孟子E章 2010-11-18
  • 打赏
  • 举报
回复
不要发相同的帖子!

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧