What are you writing it to? TextWriter? flow? what?
The following saves the object (well, it replaces it with the hexadecimal equivalent), but if you do the same with StringWriter, it detects unicode and uses this instead:
XmlDocument doc = new XmlDocument(); doc.LoadXml(@"<xml>™</xml>"); using (MemoryStream ms = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.GetEncoding("ISO-8859-1"); XmlWriter xw = XmlWriter.Create(ms, settings); doc.Save(xw); xw.Close(); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); }
Outputs:
<?xml version="1.0" encoding="iso-8859-1"?><xml>™</xml>
source share