我的代码如下
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\"?>");这句话注释了,但是注释后发现问题还是有,还是不能正常排序,所以想请各位大侠帮帮忙,指点一下。
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\"?>");这句话注释了,但是注释后发现问题还是有,还是不能正常排序,所以想请各位大侠帮帮忙,指点一下。
解决方案 »
- 怎么在类中修改窗体控件的属性
- 关于流的,在线等
- Asp.net低级问题.....
- 新手上路:谁能一步步教我如何做基于winform的图表(曲线图),工具vs.net C#
- 终于把.Net的大部分开源项目整理出来了,希望对大家有用.
- 一段C#写的文件下载程序,高手帮忙解决,谢谢
- WinForm如何将本地的变化实时的反映到远端界面的图片上
- 用户控件的引用与取值
- 綁定到主從表時在從表中浏览記錄時出現的問題.
- 请问c#下有把一个文件写成byte[]的方法么?java下很好实现的说.......
- 各位大哥 帮助datagridview 绑定dataset 做数据的修改增加
- 快速判断byte[] 中指定byte[] 的位置
麻烦来个人帮我把我昨天发的帖子顶下,问题已经自己解决了,但是没任回复.总不能无满意结贴吧,来者有分.谢谢!
http://topic.csdn.net/u/20090312/09/20c77597-eeba-43cf-a87c-73c0bf1c0b6c.html?762718896
StringBuilder sb = new StringBuilder();
ds.WriteXml(new System.IO.StringWriter(sb), XmlWriteMode.DiffGram);
{
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();
}
}
#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