3

I searched many places for the answer but couldn't find it.

I'm using a XmlSerializer to generate an XML, and I need it in ISO-8859-1 encoding. I managed to do it this way:

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();
}

The xml is OK, but the system where I need to send the XML to is case-sensitive in the encoding word, so it just accepts my file it it says encoding="ISO-8859-1" and doesn't when it says "iso-8859-1".

What can I do? Thank you in advance.

Kunstmann
  • 109
  • 1
  • 10
  • 3
    Aside from anything else, have you notified the other end that they're kinda broken? From the XML spec: " XML processors should match character encoding names in a case-insensitive way" – Jon Skeet Aug 10 '15 at 17:46
  • You'll probably need to filter the output of the serializer – ikegami Aug 10 '15 at 17:49
  • 1
    Looked like the XML serializer is using `Encoding.WebName`. You could write an `Encoding` implementation which delegates all the methods to the real one, but supplies its own `WebName`... – Jon Skeet Aug 10 '15 at 17:49
  • You can do it manually. Have a look at this [answer][1] in SO [1]: http://stackoverflow.com/questions/16706931/how-to-force-xdocument-to-output-the-prolog-in-uppercase-while-preserving-identa – hjgraca Aug 10 '15 at 17:49
  • OK, after a while I found an answer here http://stackoverflow.com/questions/398621/system-text-encoding-getencodingiso-8859-1-throws-platformnotsupportedexcept – Kunstmann Aug 10 '15 at 17:51
  • I was suffering with same problem for while and [Dimitris Answer](http://stackoverflow.com/a/31925989/2628285) was actually very good and easy tweak I just couldn't thought. @Kunstmann just interested to know if the service you are requesting is Prinetti? – Repeat Spacer Sep 22 '15 at 10:45

2 Answers2

3

Just for reference (and for my future self) here's the solution as described in 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);
    }
}
Community
  • 1
  • 1
Leonardo Herrera
  • 8,388
  • 5
  • 36
  • 66
2

Why don't you just get the XML content in a string and then use string.Replace to replace the ISO-8859-1 to iso-8859-1