Wednesday, September 3, 2008

Gathering all my posts in one place

hey guys i thought i should gather all my posts to one place so from now on i'll post everything here and also trying to recollect the things that i posted to different websites

Wednesday, February 6, 2008

Serialization and Deserialization

Serialization:

Converting an object instance to XML which can be transferred over the network or can be stored as a file


Deserialization:

Converting the XML back to Object

Example:

Writing a class which can be Serializable or Deserializable by just putting an attribute of <serializable()> on it

<serializable()> _
Public Class Authentication
Dim m_UserId As String
Dim m_Password As String
Public Property UserID() As String
Get
Return m_UserId
End Get
Set(ByVal value As String)
m_UserId = value
End Set
End Property
Public Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
End Class

Serializing
Imports System.Xml.Serialization NameSpace

'Creating Object Instance of the Class
Dim objAuth As New Authentication

'Setting Property Value
objAuth.UserID = "test"

'Setting Property Value
objAuth.Password = "test"

'Createing the object of XMLSerializer Class with the type of object to be seralized
Dim ser As New XmlSerializer(objAuth.GetType)

'Creating a memory Stream, you can user network stream or file stream also
Dim oStream As New System.IO.MemoryStream

'Serializing the Object and writing to the stream
ser.Serialize(oStream, objAuth)
DeSerializing

Imports System.Xml.Serialization NameSpace

'Creating new Object Instance of the Class
Dim newAuth As New Authentication

'Deserializing the Stream and holding in Object
newAuth = ser.Deserialize(oStream)