本文介绍了为什么我不能设置的XDocument XDeclaration编码类型为ISO-8859-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不下面的代码设置XML声明编码类型?它始终将编码设置为UTF-16来代替。我失去的东西很明显?



  VAR XDOC =新的XDocument(
新XDeclaration(1.0,异-8859-1,NULL),
新的XElement(根,)
);



输出:

 <?XML版本=1.0编码=UTF-16>?; 
<根和GT;< /根>


解决方案

请参阅有关指定的TextWriter 的编码



顺便说一句: ISO-8859-1 是一个字符集,而不是一个编码。 的Unicode 也是一个字符集,但 UTF-16 的Unicode编码字符集为一个字节序列。您不能指定文档的编码为 ISO-8859-1 ,就像你不能指定文档的字符集为 UTF-16 。需要注意的是的Unicode 是本地字符集和 UTF-16 是本地 Unicode的编码为.NET和Java 字符串类和基于文本的或基于字符串的操作。


Why doesn't the following code set the XML declaration encoding type? It always sets the encoding to utf-16 instead. Am I missing something very obvious?

var xdoc = new XDocument(
  new XDeclaration("1.0", "iso-8859-1", null), 
  new XElement("root", "")
);

output:

<?xml version="1.0" encoding="utf-16"?>
<root></root>
解决方案

See the answer about specifying the TextWriter's encoding.

As an aside: ISO-8859-1 is a character-set, not an encoding. Unicode is also a character-set, but UTF-16 is an encoding of the Unicode character set into a sequence of bytes. You cannot specify a document's encoding as ISO-8859-1, just as you cannot specify a document's character-set as UTF-16. Note that Unicode is the native character-set and UTF-16 is the native Unicode encoding for both .NET and Java String classes and text-based or string-based operations.

这篇关于为什么我不能设置的XDocument XDeclaration编码类型为ISO-8859-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:14