DataSet.WriteXml(String)生成的Xml文件中包含XML 声明, 而DataSet.WriteXml(Stream)却不会写入Xml声明即<?xml version="1.0" standalone="yes"?>
详细情况:
详细情况:
在写博客园的数据备份程序时,本来想通过ds.WriteXml(Response.OutputStream)直接将Xml数据发送到客户端,可是这样在客户端得到的Xml文件中的所有中文全是乱码,乱码的Xml文件与正常的Xml文件区别就是少了一行Xml声明。然后, 我改了代码, 手动写入Xml声明, 乱码问题就解决,代码如下:
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
writer.WriteStartDocument();
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();