为什么总的XmlWriter允许输出UTF-16编码? [英] Why is the XmlWriter always outputing utf-16 encoding?

查看:107
本文介绍了为什么总的XmlWriter允许输出UTF-16编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的扩展方法

    public static string SerializeObject<T>(this T value)
    {
        var serializer = new XmlSerializer(typeof(T));           
        var settings = new XmlWriterSettings
                       {
                        Encoding = new UTF8Encoding(true), 
                        Indent = false, 
                        OmitXmlDeclaration = false,
                        NewLineHandling = NewLineHandling.None
                       };

        using(var stringWriter = new StringWriter()) 
        {
            using(var xmlWriter = XmlWriter.Create(stringWriter, settings)) 
            {
                serializer.Serialize(xmlWriter, value);
            }

            return stringWriter.ToString();
        }
    }



但每当我把这个它有一个编码 UTF-16 规定,即<?XML版本=1.0编码=UTF-16> 。我在做什么错了?

but whenever I call this it has an encoding of utf-16 specified, ie <?xml version="1.0" encoding="utf-16"?>. What am I doing wrong?

推荐答案

字符串是UTF-16,所以写入的StringWriter将始终使用UTF-16。如果这不是你想要的,然后使用一些其他的的TextWriter 派生类中,与你喜欢的编码。

Strings are UTF-16, so writing to a StringWriter will always use UTF-16. If that's not what you want, then use some other TextWriter derived class, with the encoding you like.

这篇关于为什么总的XmlWriter允许输出UTF-16编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆