How to use Serialization.

S

Sangeetha Nagaraj

I am developing a project for a courier service. My Manager suggested to use serialization.
I dont know anything about serialization in VB.NET. Can anyone give me some details about Serialization and how to use it practically...
 
J

John Paul. A

Hi Sang,
Youe manager has given a good suggestion. Here's the details.

Serialization is the process by which objects or values are converted into a format that can be persisted or transported. Serialization allows you to save the state of objects from memory to a storage medium such as files. You can also use serialization to transport objects and values across the network. To read the state of the objects or values that you persist or transport using serialization, you use another process called deserialization. Deserialization is complementary to serialization. The .NET Framework supports two types of serialization, binary serialization and XML serialization.

Binary serialization converts the state of an object by writing the public and private fields, name of the class, and the assembly containing the class into a stream of bytes. To make your objects serializable, you need to mark the class with the Serializable attribute.

For more details.. See this...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/objserializ.asp

http://www.15seconds.com/issue/020903.htm
 
J

John Paul. A

Here's a sample and a simple code, you asked..

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Public Class SerializeDemo
Public Shared Sub Main()
Dim obj as New MySerializableClass()
obj.n1 = 10
obj.s1 = "Hello"
Dim stream As New FileStream("MyFile.dat", FileMode.Create)
Dim formatter As New BinaryFormatter()
formatter.Serialize(stream, obj)
End Sub
End Class


==========


Imports System.IO
Imports System.Xml.Serialization

Public Class SerializeDemo
Public Shared Sub Main()
Dim obj As New MySerializableClass()
obj.n1 = 10
obj.s1 = "Hello"
Dim stream As New FileStream("MyFile.xml", FileMode.Create)
Dim formatter As New XmlSerializer(GetType(MySerializableClass))
formatter.Serialize(stream, obj)
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top