zoukankan      html  css  js  c++  java
  • 使用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();

    }

  • 相关阅读:
    在酷热的就业天气寻找几丝凉意
    《Orange ’ s :一个操作系统的实现 》作者自序
    Windows的设备驱动框架
    Windows的设备驱动框架中的上层与下层模块
    如何搭建自己的开发环境
    数据库设计指南(二)设计表和字段
    软件敏捷架构师
    Struts到JSF/Tapestry
    敏捷开发
    barcode制作条形码及破解
  • 原文地址:https://www.cnblogs.com/jans2002/p/843843.html
Copyright © 2011-2022 走看看