C# 以大写形式写入单词 "ISO-8859-1"

标签 c# encoding iso-8859-1

我找了很多地方都没有找到答案。

我正在使用 XmlSerializer 生成 XML,我需要它采用 ISO-8859-1 编码。我设法这样做了:

var encoding = Encoding.GetEncoding("ISO-8859-1");
using (StreamWriter writer = new StreamWriter(outfile, appendMode, encoding))
{
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");
    serializer.Serialize(writer, DTE, ns);
    writer.Close();
}

xml 没问题,但我需要将 XML 发送到的系统在编码字中区分大小写,因此它只接受我的文件,它说 encoding="ISO-8859-1"而不是当它说“iso-8859-1”时。

我能做什么?提前谢谢你。

最佳答案

仅供引用(以及我 future 的自己),这是 Jon Skeet's comment 中描述的解决方案:

internal class Iso88591Encoding : Encoding
{
    private readonly Encoding _encoding;

    public override string WebName => _encoding.WebName.ToUpper();

    public Iso88591Encoding()
    {
        _encoding = GetEncoding("ISO-8859-1");
    }

    public override int GetByteCount(char[] chars, int index, int count)
    {
        return _encoding.GetByteCount(chars, index, count);
    }

    public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
    {
        return _encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
    }

    public override int GetCharCount(byte[] bytes, int index, int count)
    {
        return _encoding.GetCharCount(bytes, index, count);
    }

    public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
    {
        return _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
    }

    public override int GetMaxByteCount(int charCount)
    {
        return _encoding.GetMaxByteCount(charCount);
    }

    public override int GetMaxCharCount(int byteCount)
    {
        return _encoding.GetMaxCharCount(byteCount);
    }
}

关于C# 以大写形式写入单词 "ISO-8859-1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925851/

相关文章:

c# - 以整数数组为键的字典

utf-8 - ISO-8859-1 到 UTF8 的转换

c# - 使用 .NET 如何将包含 Latin-1 重音字符的 ISO 8859-1 编码文本文件转换为 UTF-8

actionscript-3 - 有什么方法可以将ActionScript 3中的常规字符串转换为Latin-1字符代码的ByteArray?

c# - 有什么简单的方法可以将数据导出为多种格式吗?

c# - 查找两个 XML 文件之间的差异

javascript - UTF-8 到 UTF-16LE Javascript

encoding - JasperReports 不显示 UTF-8 字符 - MacOSX

C# 从 Byte[512] 的末尾删除空值

mysql - 无法使用 Rstudio 在控制台中正确编码中文