The XML output will contain a header based on the encoding of the stream, not the encoding specified in the settings. Since the strings are 16-bit Unicode, the encoding will be UTF-16. The workaround is to suppress the header and add it yourself:
writerSettings.OmitXmlDeclaration = true;
Then, when you get the result from StringBuilder:
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + sb.ToString();
Guffa
source share