public static string Serialize <T>(T obj) { var stringWriter = new Utf8Writer(); var serializer = new XmlSerializer(typeof(T)); using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings() { Encoding = Encoding.UTF8 })) { serializer.Serialize(writer, obj); return(stringWriter.ToString()); } }
public string Save() { // StringWriter.Encoding always returns UTF16. We need it to return UTF8, so the // XmlDocument will write the UTF8 header. Utf8Writer sw = new Utf8Writer(); sw.NewLine = newLine; doc.Save(sw); string txt = sw.ToString(); if (endsWithEmptyLine && !txt.EndsWith(newLine)) { txt += newLine; } return(txt); }
public string Save() { // StringWriter.Encoding always returns UTF16. We need it to return UTF8, so the // XmlDocument will write the UTF8 header. Utf8Writer sw = new Utf8Writer(); sw.NewLine = newLine; doc.Save(sw); string txt = sw.ToString(); if (endsWithEmptyLine && !txt.EndsWith(newLine)) txt += newLine; return txt; }