i have the System.Xml.XmlDocument object. I want to change the xml Encoding from UT16 to UTF8
how do i do it?
i have the System.Xml.XmlDocument object. I want to change the xml Encoding from UT16 to UTF8
how do i do it?
XmlDocument uses it's own (DOM-based) internal representation of the XML; the encoding only comes into play when the XML is writen to and stored as text somewhere. You can use the XmlDocument.WriteTo method and provide an XmlWriter
configured using an XmlWriterSettings
passed in to XmlWriter.Create
. There is an XmlWriterSettings.Encoding property where you can specify UTF8.
For example:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;
using (XmlWriter xmlWriter = XmlWriter.Create(filename, xmlWriterSettings))
{
XmlDocument.WriteTo(xmlWriter);
}