I'm working on a web service in WCF.
I want to return a xml file, in UTF8.
My code is :
StringBuilder output = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 };
using (XmlWriter writer = XmlWriter.Create(output, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("Response", Resources.nameSpaceUri);
/* Some functions */
writer.WriteEndElement();
writer.WriteEndDocument();
}
return output.ToString();
But the result is something like :
<?xml version="1.0" encoding="utf-16"?>
<Response xmlns="http://namespace">
<!-- Content -->
</Response>
Why my response is in UTF-16 ?
It's simply because StringBuilder (and String, Char, char) use UTF-16. The writer declares the encoding appropriately. If you use a writer that targets a file or stream then it uses the encoding from the settings.