public static string GetString(object obj, XmlSerializerOptions options) { var serializer = GetSerializer(obj.GetType()); var settings = new XmlWriterSettings() { Encoding = Encoding.UTF8 }; if (options.HasFlag(XmlSerializerOptions.Indent)) { settings.Indent = true; } if (options.HasFlag(XmlSerializerOptions.OmitXmlDeclaration)) { settings.OmitXmlDeclaration = true; } using (var sw = new Utf8StringWriter()) using (var writer = XmlWriter.Create(sw, settings)) { if (options.HasFlag(XmlSerializerOptions.OmitNamespace)) { var ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); serializer.Serialize(writer, obj, ns); } else { serializer.Serialize(writer, obj); } writer.Close(); return(sw.ToString()); } }
public static void Write(string filename, object obj, XmlSerializerOptions options) { using (var stream = FileUtility.OpenWrite(filename)) { Write(stream, obj, options); } }