1

I have to generate a XML file using "ISO-8859-1" encoding from my Asp.Net Web API application but MemoryStream lowercases the encoding attribute from the generated XML definition to "iso-8859-1".

This method generates a XML file based on a object which has been created by a XSD.

 public static MemoryStream GenerateXml<T>(T entity) where T : class
 {
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    //Add an empty namespace and empty value
    ns.Add("", "");

    var memoryStream = new MemoryStream();
    var streamWriter = new StreamWriter(memoryStream, Encoding.GetEncoding("ISO-8859-1"));
    var serializer = new XmlSerializer(typeof(T));

    serializer.Serialize(streamWriter, entity, ns);

    return memoryStream;
 }

Then I need to use XDocument to replace the prefix definition of XML elements (Its a prerequisite that all elements should be only named with their own tags). So I had to do this:

 public MemoryStream GenerateXmlOpening<T>(T entity) where T : class
{
    var xmlMemStream = XmlHelper.GenerateXml(entity);

    xmlMemStream.Position = 0;
    XDocument doc = XDocument.Load(xmlMemStream, LoadOptions.PreserveWhitespace);
    //Removes the namespace declaration as prefix on elements
    doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();

    //the memory stream retreived from 'xmlMemStream' is already with "iso-8859-1 in lowercase, so im trying to override it
    doc.Declaration.Encoding = "ISO-8859-1";

    MemoryStream stream = new MemoryStream();

    // when i save the xdoc to the new memorystream, the encoding goes from "ISO-8859-1" to "iso-8859-1" again.
    doc.Save(stream);
    stream.Position = 0;

    return stream;
}

This is the beginning of the returned generated XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
... content

How it's supposed to be:

<?xml version="1.0" encoding="ISO-8859-1"?>
... content

Ps.* Im writing the XML using a MemoryStream because I have to write a .zip file and return a response of all generated XML files within this zip. This .Zip generator receives a list of MemoryStreams.

Rafael A. M. S.
  • 637
  • 1
  • 6
  • 27
  • _"MemoryStream lowercases the encoding attribute"_ - that sounds a bit odd, do you have a simple repro? – stuartd Dec 20 '16 at 13:44
  • Its weird as that, whenever i try to save a XML file on a MemoryStream using "ISO-8859-1" with the "ISO" word in upper case, the result comes as "iso-8859-1" in lower case, I'll try to improve the title. – Rafael A. M. S. Dec 20 '16 at 13:47
  • Why you want your encoding to be uppercase? It's perfectly valid as lowercase. The specs recommend upper, but allow for lower. If this is a problem, then the consuming side is using a non-compliant parser. – spender Dec 20 '16 at 13:57
  • "XML processors SHOULD match character encoding names in a case-insensitive way" https://www.w3.org/TR/xml/#NT-EncodingDecl – spender Dec 20 '16 at 14:00
  • 2
    Because im generating a file that should be sent to the government through an app made by them, and this app validates the generated xml file. I dont know why , but their validator is not allowing encoding attribute value to be in lowercase. Whenever i change the encoding to ISO-8859-1 in uppercase, the application is allowed to receive my XML file. – Rafael A. M. S. Dec 20 '16 at 14:00
  • Looks like you're stuck with search&replace: http://stackoverflow.com/questions/16706931/how-to-force-xdocument-to-output-the-prolog-in-uppercase-while-preserving-identa – spender Dec 20 '16 at 14:01
  • @spender thank you on your effort. As you can see, Im already manually setting the encoding on XDocument as a workaround. The problem is : When i save the XDocument back again into a MemoryStream, the MemoryStream turns the encoding to lower case. Thats the weird part. – Rafael A. M. S. Dec 20 '16 at 14:06
  • 2
    @RafaelA.M.S. It's not the MemoryStream. That's just a dumb array of bytes. `doc.Save` is the culprit here. It seems MS just write out the encoding in lowercase. The only way to address this (from the linked question above) is to hack around it. – spender Dec 20 '16 at 14:09
  • So, I'll try to workaround this using the answer you linked. Thanks – Rafael A. M. S. Dec 20 '16 at 14:13

0 Answers0