/// <summary> /// Serialize the given <see cref="UserConfig"/> /// </summary> /// <param name="config">The Config instance.</param> /// <returns>A string of serialized XML.</returns> public static string Serialize(UserConfig config) { var serializer = new XmlSerializer(config.GetType()); using (var stream = new MemoryStream()) { var settings = new XmlWriterSettings() { Encoding = new UTF8Encoding(false), Indent = false, NewLineOnAttributes = false }; using (XmlWriter writer = XmlWriter.Create(stream, settings)) { serializer.Serialize(writer, config); return(Encoding.UTF8.GetString(stream.ToArray())); } } }
/// <summary> /// Deserialize an XML string into a <see cref="UserConfig"/> model /// </summary> /// <param name="raw">An XML string.</param> /// <returns><see cref="UserConfig"/> Model</returns> public static UserConfig Deserialize(string raw) { var config = new UserConfig(); if (string.IsNullOrEmpty(raw)) { return(config); } var serializer = new XmlSerializer(config.GetType()); using (var reader = new StringReader(raw)) { try { return((UserConfig)serializer.Deserialize(reader)); } catch (InvalidOperationException) { return(config); } } }