none
XmlWriter.WriteAttributeString() not converting to UTF-8 RRS feed

  • Question

  • I have the following code to create an XML file. Why is it not doing a UTF-8 conversion on the string passed in?

    using System.IO;
    using System.Text;
    using System.Xml;
    
    namespace TestXmlWriter
    {
        class Program
        {
            private static readonly Encoding utf8NoBOM = new UTF8Encoding(false);
    
            static void Main(string[] args)
            {
                Stream stream = new FileStream("c:\\temp\\test.xml", FileMode.Create, FileAccess.ReadWrite);
                XmlWriter xmlWriter = new XmlTextWriter(stream, utf8NoBOM);
                xmlWriter.WriteStartDocument();
    
                xmlWriter.WriteStartElement("bear");
                xmlWriter.WriteAttributeString("version", "CONSULTOR_TÉCNICO");
    
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();
                xmlWriter.Flush();
                stream.Close();
            }
        }
    }

    Output:

    <?xml version="1.0" encoding="utf-8"?><bear version="CONSULTOR_TÉCNICO" />

    What do I have to do to get correct UTF-8 output?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, January 28, 2019 2:13 PM

Answers

  • The program writes the UTF-8 sequence “C3 89” for “É”. Do you expect a different result?


    • Edited by Viorel_MVP Monday, January 28, 2019 9:57 PM
    • Marked as answer by DavidThi808 Monday, January 28, 2019 10:29 PM
    Monday, January 28, 2019 9:56 PM

All replies