Generic serialization/deserialization

        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));
            }
        }
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *