Search

Serialize/ De-Serialize

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called de-serialization.
It allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
    /// 
    /// Serializes an Object
    /// 
    /// Object to SerializedObject
    /// serialized string(xml)
    public string SerializeObject(Object objectInstance)
    {
        System.IO.StringWriter stringwriter = new System.IO.StringWriter();
        System.Xml.Serialization.XmlSerializer serilizer = new System.Xml.Serialization.XmlSerializer(objectInstance.GetType());
        serilizer.Serialize(stringwriter, objectInstance);
        return stringwriter.ToString();
    }
To de-serialize it back to the object,
    /// 
    /// De-Serializes an Object
    /// 
    /// serialized string(xml)
    /// 
    public Object DeSerializeObject(string serializedObject)
    {
        var stringReader = new System.IO.StringReader(serializedObject);
        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Object));
        return serializer.Deserialize(stringReader) as Object;
    }