What is the difference between encoding and serialization in .net?

Showing Answers 1 - 3 of 3 Answers

Rajesh K

  • Feb 13th, 2007
 

Serialization :------------------Serialization in .NET allows the programmer to take an instance of an object and convert it into a format that is easily transmittable over the network, or even stored in a database or file system. This object will actually be an instance of a custom type, including any properties or fields you may have set. A few examples I can think of include the ability to send an instance of your object to another portion of a local or remote application, such as over a Web service. Another example of when to choose to serialize objects is for data within objects that would normally be stored in a database, but these pieces of data do not need to be stored individually (their own fields in your tables). Streamline the database design by only holding those pieces of data for each record you need to query on, and the rest of the pieces can simply be serialized within a custom type and stored as a complete object within the database.Advantages of Binary Serialization --------------------------------------------All members, no matter if they are read-only will be serialized. Greater performance* Advantages of XML Serialization -------------------------------------------Greater flexibility of object sharing and usage (interoperability) No strict binary dependence Human readable #region Binary Serializerspublic static System.IO.MemoryStream SerializeBinary(object request) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); serializer.Serialize(memStream, request); return memStream;}public static object DeSerializeBinary(System.IO.MemoryStream memStream) { memStream.Position=0; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object newobj = deserializer.Deserialize(memStream); memStream.Close(); return newobj;}#endregion#region XML Serializerspublic static System.IO.MemoryStream SerializeSOAP(object request) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); serializer.Serialize(memStream, request); return memStream;}public static object DeSerializeSOAP(System.IO.MemoryStream memStream) { object sr; System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); memStream.Position=0; sr = deserializer.Deserialize(memStream); memStream.Close(); return sr;}#endregion

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions