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

前端 未结 3 1242
太阳男子
太阳男子 2021-01-12 14:25

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?

v         


        
相关标签:
3条回答
  • 2021-01-12 14:59

    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.

    0 讨论(0)
  • 2021-01-12 15:01

    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.

    0 讨论(0)
  • 2021-01-12 15:08

    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

    0 讨论(0)
提交回复
热议问题