Jans的BLOG
Jans的BLOG

要在内存里处理写XML,使用了一下的代码:

  private string SerializeData()
        {
            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;  // 为什么没有效果?
            settings.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                XmlSerializer serializer =
                        new XmlSerializer(typeof(MyClass));
                serializer.Serialize(writer, anInstanceOfMyClass);
                writer.Flush();
                writer.Close();
            }
            return sb.ToString();
        }

可是无论在settings 如何设置,都输出了utf-16的属性

 

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" GeneratedAt="2006-04-18 03:28:26">
...

原来

.NET strings are always UTF-16, and so StringBuilder always builds a UTF-16 string.  So in that case you cannot override the encoding.  If you want an in memory array of encoded bytes then use new StreamWriteR(new MemoryStream ()) then get the buffer from the MemoryStream.

解决方法很多

最简单的,将StringBuilder生成的xml进行替换

xml.Replace("utf-16","utf-8");

或者

不用StringBuilder改用Stream:

   private string SerializeData()
        {
            string xml;
            using (MemoryStream ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(sw, settings))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
                    serializer.Serialize(writer, anInstanceOfMyClass);
                    writer.Flush();
                    writer.Close();
                }
                using (StreamReader sr = new StreamReader(ms))
                {
                    ms.Position = 0;
                    xml = sr.ReadToEnd();
                    sr.Close();
                }
            }
            return xml;
        }

这种方法显得罗嗦了些

还有就是在使用XmlWriter时,使用WriteProcessingInstruction显式的指定xml头

using(XmlWriter writer = XmlWriter.Create(obj))

{

                writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"");
                writer.WriteStartElement("root");
                writer.WriteStartElement("example_element");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();

}

posted on 2007-08-05 16:32  Jans  阅读(3747)  评论(1编辑  收藏  举报