// -------------------------------------------------------------------------------------------------------------------------- public string ToString(Encoding encoding) { XmlSerializer serial = new XmlSerializer(typeof(T)); using (var writer = new CustomStringWriter(encoding)) { serial.Serialize(writer, this); return(writer.ToString()); } }
// -------------------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns a document as a string that follows the given encoding rules. /// </summary> /// <param name="encoding">Optional. If omitted, the system will default to UTF-8</param> public static string ConvertToString(XDocument doc, Encoding encoding = null) { if (encoding == null) { encoding = System.Text.Encoding.UTF8; } using (CustomStringWriter writer = new CustomStringWriter(encoding)) { // The encoding that is listed in the XML declaration will be adjusted to 'encoding' doc.Save(writer); return(writer.GetStringBuilder().ToString()); } }