日期:2014-05-19  浏览次数:21027 次

如何改变生成的xml的编码方式.
序列化后生成的XML的头部
<?xml   version= "1.0 "   encoding= "utf-16 "?>
我想让生成的XML的编码格式
<?xml   version= "1.0 "   encoding= "utf-8 "?>
怎么办

代码如下
public   static   string   Serialization(Report   report)
{
StringBuilder   sb   =   new   StringBuilder();
TextWriter   writer   =   new   StringWriter(sb);
XmlSerializer   serializer   =   new   XmlSerializer(report.GetType());
serializer.Serialize(writer,report);
return   sb.ToString();
}


------解决方案--------------------
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString(););
XmlDeclaration dec = doc.FirstChild as XmlDeclaration;
dec.Encoding = "utf-8 ";
return doc.OuterXml;