static string DocumentToText(XmlDocument document) { MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); document.WriteTo(writer); writer.WriteEndDocument(); writer.Flush(); UTF8Encoding encoding = new UTF8Encoding(); return encoding.GetString(stream.ToArray()); }