public static void Serialize<T>(String file, T data) { XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, data); File.WriteAllText(file, writer.ToString()); } } public static T Deserialize<T>(String file) { String xml = File.ReadAllText(file); XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StringReader reader = new StringReader(xml)) { return (T)(serializer.Deserialize(reader)); } }
Generic serialization/deserialization
Bookmark the permalink.