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

    }

  • 相关阅读:
    Linux dd 命令
    excel合并单元格数据读取
    判断字符串是否以中文字符开头
    列表嵌套字典去重统计
    【转载】【DBDK】dpdk大页内存原理
    【LinuxShell】ps 命令浅析
    【LinuxShell】free 命令详解
    【网络安全】IOC概念浅析
    【转载】【网络安全】渗透中 PoC、Exp、Payload 与 Shellcode 的区别
    【SVN】windows 下的SVN常见问题及其解决方法
  • 原文地址:https://www.cnblogs.com/jans2002/p/843843.html
Copyright © 2011-2022 走看看