Tuesday, 11 September 2012

Beginning .Net , .Net Tips : Binary Deserialization with C# Examples and VB.Net Examples

Download Sample Application. Download...

Binary Deserialization is a reverse process of Binary Serialization.
In Binary Deserialization we fetch binary file that generated from Binary Serialization and get back orignal object.
In previous example we serialize one list object using binary serialization and store in file.

Here is example for this.
In this example we take binary serialized file and generate list object from that with data.
You can see in the output image that "booklist" object fill from the "books.bin" binary file.

C# Examples :
    [Serializable]
    public class Books
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }
        public int PublisherId { get; set; }
    }
       
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Read Binary Serialized File
        System.IO.FileStream objFS = System.IO.File.OpenRead(Server.MapPath("books.bin"));
        BinaryFormatter objBF = new BinaryFormatter();
        // Deserialize File stream into object
        var bookslist = objBF.Deserialize(objFS);

    }
VB.net Examples :
    <Serializable()> _
    Public Class Books
        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
        Public Property PublisherId As Integer
    End Class

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ''Read Binary Serialized File
        Dim objFS As System.IO.FileStream = System.IO.File.OpenRead(Server.MapPath("books.bin"))
        Dim objBF As New BinaryFormatter()
        '' Deserialize File stream into object
        Dim bookslist = objBF.Deserialize(objFS)
    End Sub

Output : 

(To view original image , click on image)


Click Here to view "Serialize object and store in file using Binary Serialization with C# Examples and VB.Net Examples". Click Here...

This is very useful .Net Tips.

For Beginning .Net articles. Click Here...

To learn more regarding XML. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



No comments:

Post a Comment