System.Text.Encoding.GetEncoding(“iso-8859-1”)抛出PlatformNotSupportedException?

请参阅主题,请注意,此问题仅适用于.NET compact框架。 这种情况发生在Windows Mobile 6 Professional SDK以及我的英文HTC Touch Pro(所有.NET CF 3.5)附带的仿真器上。 iso-8859-1代表西欧(ISO),它可能是除了us-ascii之外最重要的编码(至少当用户网帖的数量时)。

我很难理解为什么不支持这种编码,而支持以下版本(同样在模拟器和我的HTC上):

  • iso-8859-2(中欧(ISO))
  • iso-8859-3(拉丁语3(ISO))
  • iso-8859-4(波罗的海(ISO))
  • iso-8859-5(西里尔文(ISO))
  • iso-8859-7(希腊语(ISO))

那么,支持说希腊语比支持德语,法语和西class牙语更重要吗? 任何人都可以对此有所了解吗?

谢谢!

安德烈亚斯

我会尝试使用“windows-1252”作为编码字符串。 根据维基百科 ,Windows-1252是ISO-8859-1的超集。

System.Text.Encoding.GetEncoding(1252) 

这篇MSDN文章说:

.NET Compact Framework支持所有设备上的字符编码:Unicode(BE和LE),UTF8,UTF7和ASCII。

对代码页编码的支持有限,并且只有在设备的操作系统识别编码时才有。

如果设备上没有所需的编码,.NET Compact Framework将抛出PlatformNotSupportedException。

我相信所有(或至少很多)ISO编码都是代码页编码,属于“有限支持”规则。 作为替代品,UTF8可能是您最好的选择。

我稍后知道它,但是我为。编译ISO-8859-1的.net cf做了一个实现,我希望这可以帮助:

 namespace System.Text { public class Latin1Encoding : Encoding { private readonly string m_specialCharset = (char) 0xA0 + @"¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"; public override string WebName { get { return @"ISO-8859-1"; } } public override int CodePage { get { return 28591; } } public override int GetByteCount(char[] chars, int index, int count) { return count; } public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { if (chars == null) throw new ArgumentNullException(@"chars", @"null array"); if (bytes == null) throw new ArgumentNullException(@"bytes", @"null array"); if (charIndex < 0) throw new ArgumentOutOfRangeException(@"charIndex"); if (charCount < 0) throw new ArgumentOutOfRangeException(@"charCount"); if (chars.Length - charIndex < charCount) throw new ArgumentOutOfRangeException(@"chars"); if (byteIndex < 0 || byteIndex > bytes.Length) throw new ArgumentOutOfRangeException(@"byteIndex"); for (int i = 0; i < charCount; i++) { char ch = chars[charIndex + i]; int chVal = ch; bytes[byteIndex + i] = chVal < 160 ? (byte)ch : (chVal <= byte.MaxValue ? (byte)m_specialCharset[chVal - 160] : (byte)63); } return charCount; } public override int GetCharCount(byte[] bytes, int index, int count) { return count; } public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { if (chars == null) throw new ArgumentNullException(@"chars", @"null array"); if (bytes == null) throw new ArgumentNullException(@"bytes", @"null array"); if (byteIndex < 0) throw new ArgumentOutOfRangeException(@"byteIndex"); if (byteCount < 0) throw new ArgumentOutOfRangeException(@"byteCount"); if (bytes.Length - byteIndex < byteCount) throw new ArgumentOutOfRangeException(@"bytes"); if (charIndex < 0 || charIndex > chars.Length) throw new ArgumentOutOfRangeException(@"charIndex"); for (int i = 0; i < byteCount; ++i) { byte b = bytes[byteIndex + i]; chars[charIndex + i] = b < 160 ? (char)b : m_specialCharset[b - 160]; } return byteCount; } public override int GetMaxByteCount(int charCount) { return charCount; } public override int GetMaxCharCount(int byteCount) { return byteCount; } } } 

您是否尝试过大写字符集名称? 官方注册不包括您提供的小写名称(这不能解释为什么它接受其他ISO-8859变体的小写版本)。

 Name: ISO_8859-1:1987 [RFC1345,KXS2] MIBenum: 4 Source: ECMA registry Alias: iso-ir-100 Alias: ISO_8859-1 Alias: ISO-8859-1 (preferred MIME name) Alias: latin1 Alias: l1 Alias: IBM819 Alias: CP819 Alias: csISOLatin1 

很奇怪,8859-1不受支持,但是说,UTF-8确实能够代表所有8859-1个字符(以及更多),所以你不能只使用UTF- 8而不是? 这就是我们内部所做的事情,今天我刚刚处理了同样的问题。 使用UTF-8的优点是,您可以获得对远东和西里尔语言的支持,而无需进行修改,也不会增加西方语言的重量。

如果有人得到exception(.NET紧凑框架),如:

  System.Text.Encoding.GetEncoding(“iso-8859-1”) throws PlatformNotSupportedException, //Please follow the steps: byte[] bytes=Encoding.Default.GetBytes(yourText.ToString); //The code is: FileInfo fileInfo = new FileInfo(FullfileName); //file type : *.text,*.xml string yourText = (char) 0xA0 + @"¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"; using (FileStream s = fileInfo.OpenWrite()) { s.Write(Encoding.Default.GetBytes(yourText.ToString()), 0, yourText.Length); }