How can I remove the BOM from XmlTextWriter using C#? How can I remove the BOM from XmlTextWriter using C#? xml xml

How can I remove the BOM from XmlTextWriter using C#?


You're saving the file twice - once with XmlTextWriter and once with xmlDoc.Save. Saving from the XmlTextWriter isn't adding a BOM - saving with xmlDoc.Save is.

Just save to a TextWriter instead, so that you can specify the encoding again:

using (TextWriter writer = new StreamWriter(filename, false,                                            new UTF8Encoding(false)){    xmlDoc.Save(writer);}


I'd write the XML to a string(builder) instead and then write that string to file.