The process of serialization converts an object into a form so that it can be transported across the network or can be placed in a storage location. The storage location could be a physical file, database or ASP.NET Cache. The state of the Object is preserved in form so that it could be constructed again at some later stage, which is know as Deserialization.
There are three formats of serialization
- Binary Serialization - Light and compact used in Remoting
- SOAP Serialization - interoperable use SOAP and used in web Services
- XML Serialization - Custom Serialization
In this article I will put forward an example of XML Serialization.
XML Serialization
For XML serialization, For every public member you need an attribute and that is necessary. it can serialize only public members.
It is also known as Shallow Serialization.
Following Is the code for serializing while accessing the data from Database.
Dim cnnConnection As New SqlConnection()
Try
'Declare local variables
Dim drdDetails As SqlClient.SqlDataReader = Nothing
Dim objSer As New BLL.SerCollection
Dim cmdDetails As SqlCommand = Nothing
cnnConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionString["ConnectionString"].DBConnectionString
cmdDetails = New SqlCommand(PROC_SELECT_FOR_SEARCH, cnnConnection)
'Set the timeout and add parameters to the Stored Procedure
cmdDetails.CommandTimeout = 30
cmdDetails.CommandType = CommandType.StoredProcedure
cmdDetails.Parameters.Add("@Search", SqlDbType.VarChar).Value = strSearch
'Open the connection and get the query results
cnnConnection.Open()
Dim XMLR As System.Xml.XmlReader = cmdDetails.ExecuteXmlReader
Dim serializer As XmlSerializer = New XmlSerializer(GetType(BLL.SerCollection))
objSer = CType(serializer.Deserialize(XMLR), BLL.SerCollection)
Return objSer
Catch ex As Exception
'Exception Block
Throw New DBException(ex, "Get Method Faild")
Return Nothing
Finally
cnnConnection.Close()
End Try