1

I know that there is a lot of tutorials about this and even answered questions here, but I have problem I'm trying to resolve for hours and I read almost everything here, but this still remains mistery for me. Please help:

I'm creating XML, and it's created, but the problem is that encoding is UTF-16, and it should be UTF-8. This is what I tried so far, but still is UTF-16:

        var xmlText = new StringBuilder();

        using (var xml = XmlWriter.Create(xmlText))
        {
            xml.WriteStartDocument();
            xml.WriteStartElement("Weather");


            if (model.ModuleList[0] != null)
            {
                foreach (var weather in model.ModuleList)
                {
                    var AddProperty = new Action<XmlWriter, ModuleModel>((a, forc) =>
                    {
                        xml.WriteStartElement("Forecast");
                        a.WriteElementString("Description", forc.Description);
                        a.WriteElementString("Date", forc.Date.ToString());
                        a.WriteElementString("MinTemp", forc.Min_Temp.ToString());
                        a.WriteElementString("MaxTemp", forc.Max_Temp.ToString());
                        a.WriteElementString("Pressure", forc.Pressure.ToString());
                        a.WriteElementString("Humidity", forc.Humidity.ToString());                           
                        xml.WriteEndElement();
                    });
                    AddProperty(xml, weather);
                }
            }             

            xml.WriteEndElement();
            xml.WriteEndDocument();
        }
        var xmlresult = xmlText.ToString();

How to set encoding to my XML to UTF-8? Please help...

9
  • 1
    You're writing the XML into a StringWriter. It's somewhat obvious that the encoding of a StringWriter would be the native string encoding that C# uses internally, which is UTF-16. Just write your XML to a UTF-8 Writer. Commented Sep 6, 2013 at 23:20
  • So I need to try different approach...will do... thanks. Commented Sep 6, 2013 at 23:26
  • @millimoose: nitpicking - strings do not have an encoding Commented Sep 6, 2013 at 23:32
  • @MiMo Seeing how strings ultimately have to be stored in a sequence of bytes, they should have some internal encoding, unless C# stores 32-bit code points directly. Since char is 2-bytes which does not cover all of Unicode, I doubt this. Commented Sep 6, 2013 at 23:33
  • Write it to a MemoryStream, and set the encoding explicitly using XmlWriterSettings with the XmlStream.Create(Stream, XmlWriterSettings) overload. Commented Sep 6, 2013 at 23:38

1 Answer 1

1

The result of your code is a string xmlresult - and strings do not have an encoding, they are always Unicode.

You use an encoding when you convert a string to a sequence of byte - so your problem is not in the piece of code you posted, but in the code you use to write that string to a file.

Something like this:

 using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8))
 {
     writer.Write(xmlresult);
 }

will write a UTF-8 file - where filename contains the path of the file.

If you need UTF-8 encoded bytes in memory use:

var utf8Bytes = Encoding.UTF8.GetBytes("xmlresult");
Sign up to request clarification or add additional context in comments.

8 Comments

I actually need XML in UTF8 to store it in database. I'll try different approach then. I'm doing this first time so everything is new for me... Appreciate your help, thanks.
Can you please tell me what is fileName in your example above?
Which is the data type of the database column you need to write the XML to? Which database are you using?
@user2710923 Most databases should support Unicode strings / text fields.
The XML type in SQL Server handles the encoding internally - you don't have to do any conversion - and actually you don't even need to build a string, you can use an XmlReader directly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.