using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Web; using System.Text; namespace AM2R_ModPacker { /// /// The Serializer class, that serializes to and deserializes from XML files. /// public class Serializer { /// /// Serializes as a to XML. /// /// The class to serialize to. /// The object that will be serialized. /// The serialized XML as a . public static string Serialize(object item) { Type t = typeof(T); MemoryStream memStream = new MemoryStream(); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(t); serializer.Serialize(memStream, item); string xml = Encoding.UTF8.GetString(memStream.ToArray()); memStream.Flush(); memStream.Close(); memStream.Dispose(); memStream = null; return xml; } /// /// Deserialize into an object of class that can be assigned. /// /// The class that will be deserialized to. /// An XML that will be deserialized. /// A deserialized object of class from . public static T Deserialize(string xmlString) { Type t = typeof(T); using (MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(t); return (T)serializer.Deserialize(memStream); } } } }