I have an XmlDocument
and I am saving it with an XmlWriter
, using this post. Despite setting the Encoding
to Utf-8 and the file getting saved with Utf-8 encoding in fact, the xml declaration in the file has the "utf-16" as the value of the encoding
attribute.
I can't see where is the error in my code:
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings
{
Encoding=Encoding.UTF8
};
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
xDoc.Save(writer);
}
using (
StreamWriter sw = new StreamWriter(
new FileStream(strXmlName, FileMode.Create, FileAccess.Write),
Encoding.UTF8
)
)
{
sw.Write(sb.ToString());
}