locked
Using XMLSettings to set encoding to UTF8 but ... RRS feed

  • Question

  • Hi One and All,

     

    I'm using the following code snippet to set the UTF encoding for an XML file generator that I'm writing:

     

     

    StringBuilder sb = new StringBuilder();

    XmlWriterSettings xmlSettings = new XmlWriterSettings();

    xmlSettings.Encoding = Encoding.UTF8;

    XmlWriter writer = XmlWriter.Create(sb, xmlSettings);

    this._QuizTemplate.WriteQuizXML(writer);

    writer.Flush();

     

    The writer object won't grab the xmlSettings object. Stays UTF16 and this is causing an important part of this app to blow up.

     

    Any suggestions would be greatly appreciated

     

    Thanks

     

    Pat

     

    Thursday, August 9, 2007 6:42 PM

Answers

  • I've experienced the same problem.. Anyway, here is a solution that does work:

    Code Snippet

    MemoryStream memoryStream = new MemoryStream();
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();

    // If i use Encodings.UTF8 the BOM will be prepended...

    xmlWriterSettings.Encoding = new UTF8Encoding(false);
    xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
    xmlWriterSettings.Indent = true;

    XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("root", "http://www.timvw.be/ns");
    xmlWriter.WriteEndElement();
    xmlWriter.WriteEndDocument();
    xmlWriter.Flush();
    xmlWriter.Close();


    string xmlString = Encoding.UTF8.GetString(memoryStream.GetBuffer());



    Thursday, August 9, 2007 10:06 PM

All replies

  • Hi Pat,

     

    This is interesting.... when you write your Xml to the string builder it's given an encoding of "UTF16" as you've seen, however if you write to the Console.Out steam it's encoded to "ibm850"... however if you stream the xml to file then it's written in "UTF8"...

     

    This is what I think.... could be wrong but there is an equal chance that it could be right... I think the writer writes the XML in whatever encoding is valid for the stream. For example all strings in .NET are UTF16 (Unicode), so when you stream to the StringBuilder it's always UTF16, the Console.Out stream on the other hand, without knowing to much about it might only allow an old encoding (ibm850 sounds like old skool mainframe pre unicode encodings), where as a file can be any encoding and so it allows the UTF8 format.

     

    Suggestion, stream to file mate and then re-read in as a string. This should be ok as it's only when the XML is written that the encoding is decided.

     

    Hope that proves helpful.

    Thursday, August 9, 2007 7:37 PM
  • I've experienced the same problem.. Anyway, here is a solution that does work:

    Code Snippet

    MemoryStream memoryStream = new MemoryStream();
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();

    // If i use Encodings.UTF8 the BOM will be prepended...

    xmlWriterSettings.Encoding = new UTF8Encoding(false);
    xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
    xmlWriterSettings.Indent = true;

    XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("root", "http://www.timvw.be/ns");
    xmlWriter.WriteEndElement();
    xmlWriter.WriteEndDocument();
    xmlWriter.Flush();
    xmlWriter.Close();


    string xmlString = Encoding.UTF8.GetString(memoryStream.GetBuffer());



    Thursday, August 9, 2007 10:06 PM
  • StringWriter and StringBuilder default to UTF-16. You can subclass StringWriter to change that, see StringWriterWithEncoding in http://groups.google.com/group/microsoft.public.xml/browse_frm/thread/1cc84f2c49a5d5ad/2e9e6817827e96e5?lnk=st&q=StringWriterWithEncoding+author%3Ahonnen&rnum=1#2e9e6817827e96e5

     

    Friday, August 10, 2007 1:43 PM
  • Here is another solution that works
    Happy coding

    public class StringWriterWithEncoding : StringWriter
        {
            private Encoding myEncoding;
            public override Encoding Encoding
            {
                get
                {
                    return myEncoding;
                }
            }
            public StringWriterWithEncoding ( Encoding encoding )
                : base ()
            {
                myEncoding = encoding;
            }
        }

     


    XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
    ns.Add ("star", "http://www.starstandards.org/STAR");
    ns.Add ("oa", "http://www.openapplications.org/oagis");
             
    XmlWriterSettings writerSettings = new XmlWriterSettings ();
    XmlSerializer serializer = new XmlSerializer (typeof (class Object));
    StringWriter stringWriter = new StringWriterWithEncoding (Encoding.UTF8);
               
    using (XmlWriter xmlWriter = XmlWriter.Create (stringWriter, writerSettings))
    {
        serializer.Serialize (xmlWriter, Object, ns);
    }
    string xmlText =  stringWriter.ToString ();

    • Proposed as answer by stsong Tuesday, October 22, 2019 7:01 PM
    Wednesday, September 2, 2009 4:55 PM