Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Serialization

0.00/5 (No votes)
21 Dec 2008 3  
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.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

  1. Binary Serialization - Light and compact used in Remoting
  2. SOAP Serialization - interoperable use SOAP and used in web Services
  3. 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 
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here