DataSet转成XML的编码问题?

liushuhuan426 2009-03-12 03:20:47
我的代码如下
                
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);

//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);

//返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
//sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();

问题出现在用上面的方法把dataset转成XML文件后,当我再把XML转回Dataset后发现里面的字符类型数据不能正常排序,在网上找了找原因说可能是utf-8编码的问题于是我把sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");这句话注释了,但是注释后发现问题还是有,还是不能正常排序,所以想请各位大侠帮帮忙,指点一下。
...全文
227 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzxap 2009-03-19
  • 打赏
  • 举报
回复
统一用utf-8编码

charset="utf-8"
mykelly6 2009-03-19
  • 打赏
  • 举报
回复
没看出来哪有dataset啊。。。
#region write/read dataset using xml file
public static void WriteXML(string descDirectory, string fileName, DataSet ds)
{
if (!Directory.Exists(descDirectory))
{
Directory.CreateDirectory(descDirectory);
}
ds.WriteXml(descDirectory + @"\" + fileName + ".xml");
ds.WriteXmlSchema(descDirectory + @"\" + fileName + ".xsd");
}

public static DataSet ReadXML(string descDirectory, string fileName)
{
XmlDataDocument document = new XmlDataDocument();
document.DataSet.ReadXml(descDirectory + @"\" + fileName + ".xml");
return document.DataSet;

}
#endregion

rx78gp01 2009-03-19
  • 打赏
  • 举报
回复
ds.GetXml();直接称xml了
liushuhuan426 2009-03-16
  • 打赏
  • 举报
回复
楼上的有没有遇到过我说的问题
OKILOVE 2009-03-13
  • 打赏
  • 举报
回复
..ds.WriteXml(文件名) 即可,你用字节流有可能会解析不正常的
steal8275756 2009-03-13
  • 打赏
  • 举报
回复

StringBuilder sb = new StringBuilder();
ds.WriteXml(new System.IO.StringWriter(sb), XmlWriteMode.DiffGram);
wolf1118baby 2009-03-13
  • 打赏
  • 举报
回复
有个方法直接转换成xml
浪子-无悔 2009-03-13
  • 打赏
  • 举报
回复
都是高手...
麻烦来个人帮我把我昨天发的帖子顶下,问题已经自己解决了,但是没任回复.总不能无满意结贴吧,来者有分.谢谢!
http://topic.csdn.net/u/20090312/09/20c77597-eeba-43cf-a87c-73c0bf1c0b6c.html?762718896
liushuhuan426 2009-03-13
  • 打赏
  • 举报
回复
???
OKILOVE 2009-03-13
  • 打赏
  • 举报
回复
这是我们再用的一个xml转化方法

public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null;

try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);

//返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null)
writer.Close();
if (stream != null)
stream.Close();
}
}
liushuhuan426 2009-03-12
  • 打赏
  • 举报
回复
有什么解决方法?
hechenqingtian 2009-03-12
  • 打赏
  • 举报
回复
不是有个方法吗 WriteXml()
liushuhuan426 2009-03-12
  • 打赏
  • 举报
回复
???没有人能解决吗
liushuhuan426 2009-03-12
  • 打赏
  • 举报
回复
大家帮帮忙吧
zwwon 2009-03-12
  • 打赏
  • 举报
回复
试试这样呢

dataSet.WriteXml("c:/a.txt");
liushuhuan426 2009-03-12
  • 打赏
  • 举报
回复
大家帮帮忙啊
cppfaq 2009-03-12
  • 打赏
  • 举报
回复
怎么排序的?
来客心动 2009-03-12
  • 打赏
  • 举报
回复
做两次编码

111,126

社区成员

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

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

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