我的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);出错了

解决方案 »

  1.   


     //再通过查询解析出来的的字符串有没有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();
            }