• 使用StringBuilder写XML遭遇UTF16问题


    要在内存里处理写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();

    }

  • 相关阅读:
    重载
    两数之和
    求二叉树层序遍历
    最小的K的个数
    二分查找
    实现二叉树先序,中序,后序
    判断 链表中是否有环
    设计LRU缓存结构
    排序
    反转链条
  • 原文地址:https://www.cnblogs.com/jans2002/p/843843.html
Copyright © 2020-2023  润新知