David Glenn
David Glenn

Reputation: 24522

Why can't I set the XDocument XDeclaration encoding type to iso-8859-1?

Why doesn't the following code set the XML declaration encoding type? It always sets the encoding to utf-16 instead. Am I missing something very obvious?

var xdoc = new XDocument(
  new XDeclaration("1.0", "iso-8859-1", null), 
  new XElement("root", "")
);

output:

<?xml version="1.0" encoding="utf-16"?>
<root></root>

Upvotes: 7

Views: 9002

Answers (3)

floele
floele

Reputation: 3788

I somehow can't find any working answer here, so here is an actual solution which will output the wanted encoding in the header:

    private void CreateXml()
    {       
        XmlTextWriter xmlwriter = new XmlTextWriter("c:\\test.xml", Encoding.GetEncoding("iso-8859-1"));        

        XDocument xdoc = new XDocument(
          new XElement("Test")
        );

        xdoc.Save(xmlwriter);
        xmlwriter.Close();
    }

The reason why you are getting UTF-16 is that strings are encoded with UTF-16 in memory, and as long as you don't specify an encoding for the output of the XML, it will override the encoding in the XML header to match the actual encoding being used. Using an XmlTextWriter is one method of specifying a different encoding.

You can also let the XmlTextWriter write to a MemoryStream and then transform it back to string if you need to perform the whole operation in memory.

Upvotes: 1

Big Rich
Big Rich

Reputation: 6025

As stated, the .NET XML/Stream writing implementation 'picks up' or interprets the encoding from somewhere other than the declared XML encoding. I have successfully tested a working solution, as described at the URL contained within the earlier Stackoverflow post

XDocument xmlDoc = new XDocument(
        new XDeclaration("1.0", "utf-8", "no"), 
        new XElement("foo", "bar"));

MemoryStream memstream = new MemoryStream();
XmlTextWriter xmlwriter = new XmlTextWriter(memstream, new UTF8Encoding());

//'Write' (save) XDocument XML to MemoryStream-backed XmlTextWriter instance
xmlDoc.Save(xmlwriter);

//Read back XML string from stream
xmlwriter.Flush();    
memstream.Seek(0, SeekOrigin.Begin);  //OR "stream.Position = 0"
StreamReader streamreader = new StreamReader(memstream);
string xml = streamreader.ReadToEnd();

Console.WriteLine(xml);
Console.WriteLine(reader.ReadToEnd());

I hope this helps somebody.

Cheers

Upvotes: 4

yfeldblum
yfeldblum

Reputation: 65435

See the answer about specifying the TextWriter's encoding.

As an aside: ISO-8859-1 is a character-set, not an encoding. Unicode is also a character-set, but UTF-16 is an encoding of the Unicode character set into a sequence of bytes. You cannot specify a document's encoding as ISO-8859-1, just as you cannot specify a document's character-set as UTF-16. Note that Unicode is the native character-set and UTF-16 is the native Unicode encoding for both .NET and Java String classes and text-based or string-based operations.

Upvotes: 5

Related Questions