本文介绍了XML通过使用StringBuilder的序列化的XmlWriter是utf-16,同时通过流为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,当我遇到它,并写了一个控制台应用程序进行检查,并确保我没有做任何事情。



任何人都可以解释一下吗?



下面的代码:

 使用系统; 
使用System.Collections.Generic;
:使用System.IO;
使用System.Linq的;
使用System.Text;
使用的System.Xml;
使用的System.Xml.Serialization;

命名ConsoleApplication1
{
公共类节目
{
静态无效的主要(字串[] args)
{
变种O =新SomeObject {字段1 =字符串值,字段2 = 8};

Console.WriteLine(ObjectToXmlViaStringBuilder);
Console.Write(ObjectToXmlViaStringBuilder(O));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(ObjectToXmlViaStream);
Console.Write(StreamToString(ObjectToXmlViaStream(O)));
Console.ReadKey();
}

公共静态字符串ObjectToXmlViaStringBuilder(SomeObject someObject)
{
无功输出=新的StringBuilder();
VAR设置=新XmlWriterSettings {编码= Encoding.UTF8,缩进= TRUE};

使用(VAR的XmlWriter = XmlWriter.Create(输出设置))
{
无功序列化=新的XmlSerializer(typeof运算(SomeObject));
变种命名空间=新XmlSerializerNamespaces();

xmlWriter.WriteStartDocument();
xmlWriter.WriteDocType(字段1,NULL,someObject.dtd,NULL);
namespaces.Add(的String.Empty,的String.Empty);
serializer.Serialize(XmlWriter的,someObject,命名空间);
}

返回output.ToString();
}

私人静态字符串StreamToString(流流)
{
变种读者=新的StreamReader(流);
返回reader.ReadToEnd();
}

公共静态流ObjectToXmlViaStream(SomeObject someObject)
{
无功输出=新的MemoryStream();
VAR设置=新XmlWriterSettings {编码= Encoding.UTF8,缩进= TRUE};

使用(VAR的XmlWriter = XmlWriter.Create(输出设置))
{
无功序列化=新的XmlSerializer(typeof运算(SomeObject));
变种命名空间=新XmlSerializerNamespaces();

xmlWriter.WriteStartDocument();
xmlWriter.WriteDocType(字段1,NULL,someObject.dtd,NULL);
namespaces.Add(的String.Empty,的String.Empty);
serializer.Serialize(XmlWriter的,someObject,命名空间);
}

output.Seek(0L,SeekOrigin.Begin);

返回输出;
}

公共类SomeObject
{
公共字符串字段1 {搞定;组; }
公众诠释场2 {搞定;组; }
}
}
}

这是结果:



ObjectToXmlViaStringBuilder

 <?XML版本=1.0编码=UTF-16>?; 
<!DOCTYPE字段1 SYSTEMsomeObject.dtd>
< SomeObject>
<&字段1 GT;字符串值< /字段1>
<&字段2 GT; 8示/场2>
< / SomeObject>



ObjectToXmlViaStream

 <?XML版本=1.0编码=UTF-8>?; 
<!DOCTYPE字段1 SYSTEMsomeObject.dtd>
< SomeObject>
<&字段1 GT;字符串值< /字段1>
<&字段2 GT; 8示/场2>
< / SomeObject>


解决方案

当你创建一个的XmlWriter 围绕的TextWriter 的XmlWriter 总是使用基本的<$ C $的编码C>的TextWriter 。的的StringWriter 始终是UTF-16,因为这是字符串如何.NET是内部编码的编码。



当您创建一个的XmlWriter 围绕,也没有为,因此它使用了 XmlWriterSettings


指定的编码

I was surprised when I encountered it, and wrote a console application to check it and make sure I wasn't doing anything else.

Can anyone explain this?

Here's the code:

using System;    
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var o = new SomeObject { Field1 = "string value", Field2 = 8 };

            Console.WriteLine("ObjectToXmlViaStringBuilder");
            Console.Write(ObjectToXmlViaStringBuilder(o));
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("ObjectToXmlViaStream");
            Console.Write(StreamToString(ObjectToXmlViaStream(o)));
            Console.ReadKey();
        }

        public static string ObjectToXmlViaStringBuilder(SomeObject someObject)
        {
            var output = new StringBuilder();
            var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

            using (var xmlWriter = XmlWriter.Create(output, settings))
            {
                var serializer = new XmlSerializer(typeof(SomeObject));
                var namespaces = new XmlSerializerNamespaces();

                xmlWriter.WriteStartDocument();
                xmlWriter.WriteDocType("Field1", null, "someObject.dtd", null);
                namespaces.Add(string.Empty, string.Empty);
                serializer.Serialize(xmlWriter, someObject, namespaces);
            }

            return output.ToString();
        }

        private static string StreamToString(Stream stream)
        {
            var reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }

        public static Stream ObjectToXmlViaStream(SomeObject someObject)
        {
            var output = new MemoryStream();
            var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

            using (var xmlWriter = XmlWriter.Create(output, settings))
            {
                var serializer = new XmlSerializer(typeof(SomeObject));
                var namespaces = new XmlSerializerNamespaces();

                xmlWriter.WriteStartDocument();
                xmlWriter.WriteDocType("Field1", null, "someObject.dtd", null);
                namespaces.Add(string.Empty, string.Empty);
                serializer.Serialize(xmlWriter, someObject, namespaces);
            }

            output.Seek(0L, SeekOrigin.Begin);

            return output;
        }

        public class SomeObject
        {
            public string Field1 { get; set; }
            public int Field2 { get; set; }
        }
    }
}

This is the result:

ObjectToXmlViaStringBuilder

<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE Field1 SYSTEM "someObject.dtd">
<SomeObject>
<Field1>string value</Field1>
<Field2>8</Field2>
</SomeObject>

ObjectToXmlViaStream

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Field1 SYSTEM "someObject.dtd">
<SomeObject>
<Field1>string value</Field1>
<Field2>8</Field2>
</SomeObject>
解决方案

When you create an XmlWriter around a TextWriter, the XmlWriter always uses the encoding of the underlying TextWriter. The encoding of a StringWriter is always UTF-16, since that's how .NET strings are encoded internally.

When you create an XmlWriter around a Stream, there is no encoding defined for the Stream, so it uses the encoding specified in the XmlWriterSettings.

这篇关于XML通过使用StringBuilder的序列化的XmlWriter是utf-16,同时通过流为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:41