Click here to Skip to main content
15,881,172 members
Articles / Hosted Services / Storage
Article

Serialization

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 5.6K   2  
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 
 
This article was originally posted at http://wiki.asp.net/page.aspx/576/serialization

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --