1

I am trying to open an xml file in browser and I get the following error:

Switch from current encoding to specified encoding not supported. Error processing resource

Now I am generating this XML using C# by the code below. My XML has latin characters so I want to have encoding as ISO-8859-1 and not utf-16. But each time my xml generate, it take encoding as utf-16 and not ISO-8859-1. I would like to know what is the cause and resolution.

Note: I only need ISO-8859-1 and not utf-8 or utf-16 as the XML is getting generated properly if the encode is ISO-8859-1.

C# code:

var doc = new XDocument(new XDeclaration("1.0", "iso-8859-1", "yes"),new XElement("webapp", new XAttribute("name", webApp.Name), new XAttribute("id", webApp.Id), Myinformation());
  var wr= new StringWriter();
            doc.Save(wr);
            return wr.ToString();
Ankit Kumar
  • 476
  • 1
  • 11
  • 38
  • See the comments and links given in [the question you asked an hour ago](http://stackoverflow.com/questions/40278579/missing-xml-declaration-using-c-sharp-code). You need to use an `XmlWriter`. Using `ToString` or a `StringWriter` will always result in an `XmlWriter` using UTF-16. – Charles Mager Oct 27 '16 at 09:08
  • ok..will try that..sorry i missed that as the issue was different tht time – Ankit Kumar Oct 27 '16 at 09:24
  • XML doesn't support latin characters. Ankit suggestion using XmlWriter will automatically convert latin character to unicode values. See webpage : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references – jdweng Oct 27 '16 at 09:51
  • I dont mind characters getting converted to unicode values. I just want my XMl to generate on a browser without breaking at special character such as Å, Ø etc. I tried with encoding iso-8859-1 and was able to generate those characters in browser. my challenge here is C# is not allowing me to apply ISO-8589-1 coding.. it always changes to UTF-16 – Ankit Kumar Oct 27 '16 at 10:42
  • @jdweng there are only 5 'special' characters that might need escaping in XML - `< > & " '` - the rest are fair game. – Charles Mager Oct 27 '16 at 12:06
  • What you might potentially be missing is that a string doesn't have an encoding: a string is encoded when written as binary data, e.g. to a stream or file. You should probably be creating an `XmlWriter` with your response stream and the relevant encoding. – Charles Mager Oct 27 '16 at 12:12
  • @jdweng: **Of course XML supports latin characters.** You must be more precise. What you probably meant to say is that *XML does not have predefined entities for latin characters.* – kjhughes Oct 27 '16 at 12:43
  • @Charles..I couldnt find any XMLWriter example in the links mentioned in my previous question. My method returns a string value which later gets generated into bytes. The string always returns utf-16 as the encoding value and not ISO-8589-1 – Ankit Kumar Oct 27 '16 at 12:48

1 Answers1

2

A string, conceptually at least, doesn't have an encoding. It's just a sequence of unicode characters. That said, a string in memory is essentially UTF-16, hence StringWriter returns that from its Encoding property.

What you need is a writer that specifies the encoding you want:

var doc = new XDocument(new XElement("root"));

byte[] bytes;

var isoEncoding = Encoding.GetEncoding("ISO-8859-1");

var settings = new XmlWriterSettings
{
    Indent = true,
    Encoding = isoEncoding
};

using (var ms = new MemoryStream())
{
    using (var writer = XmlWriter.Create(ms, settings))
    {
        doc.Save(writer);
    }
    bytes = ms.ToArray();
}

This will give you the binary data encoded using ISO-8859-1, and as a consequence the declaration will specify that encoding.

You can convert this back to a string:

var text = isoEncoding.GetString(bytes);

But note that this is no longer encoded in ISO-8859-1, it's just a string again. You could very easily encode it using something else and your declaration would be incorrect.

If you're sure you want this as an intermediate string, then I'd suggest you use the approach in this answer.

Community
  • 1
  • 1
Charles Mager
  • 25,735
  • 2
  • 35
  • 45