0

I have a System.Xml.XmlDocument object and I want to change the xml Encoding from UT16 to UTF8. How do I do it?

1
  • The question makes no sense. UTF-8 is the default. Are you actually trying to load a .xml and convert it? Commented Jun 15, 2010 at 12:04

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.