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();

    }

  • 相关阅读:
    SQL SERVER DBCC命令参考
    Sqlserver 死锁问题
    收集面试题目DB
    收集面试题目Net
    [转]Virtual PC 2007虚拟机上安装Ubuntu 8.10桌面版
    Tcl/tk基础-2
    【转】内存详解
    [转]C# P2P与NAT技术之二
    泛型跟KeyNotFoundException
    用InstallAware 9制作BDE安装程序
  • 原文地址:https://www.cnblogs.com/jans2002/p/843843.html
Copyright © 2011-2022 走看看