Monday, 10 September 2012

Beginning .Net , .Net Tips : Serialize object and store in file using Binary Serialization with C# Examples and VB.Net Examples

 Download Sample Application. Download...

There is a need to serialize object and store into a file, and then deserialize when required.
For Binary Serialization we are using "BinaryFormatter" class which is available in "System.Runtime.Serialization.Formatters.Binary" namespace.
"BinaryFormatter" class produce a binary data of serialized object. If you want to serialize custom class object for that you have to declare that class as serializable and add "[Serializable]" attribute on that class name.

Here is example for this.
In this example we take one class name "Books" and generate books list object and do binary serialize of that list and store in "Books.bin" file. You can give any extension of that generated file name. The generated file is in binary so not easy to understand.


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; }
    }

    public List<Books> GetBooksList()
    {
        return new List<Books> {
                        new Books { Title="ASP.NET",ISBN="asp1",ReleaseDate= DateTime.Parse( "11/11/2010") ,Pages=200,PublisherId=1},
                        new Books { Title="C#.NET",ISBN="c#2",ReleaseDate= DateTime.Parse( "10/11/2010") ,Pages=500,PublisherId=1},
                        new Books { Title="VB.NET",ISBN="vb3",ReleaseDate= DateTime.Parse( "5/5/2009") ,Pages=400,PublisherId=1},
                        new Books { Title="SQL Server",ISBN="sql4",ReleaseDate= DateTime.Parse( "6/9/2010"),Pages=300,PublisherId=2 },
                        new Books { Title="JAVA",ISBN="java5",ReleaseDate= DateTime.Parse( "8/5/2011"),Pages=400,PublisherId=3 },
                        new Books { Title="HTML",ISBN="html6",ReleaseDate= DateTime.Parse( "9/5/2011"),Pages=400 }
        
        };

    }

    private void BinarySerialization(System.Collections.Generic.List<Books> bookslist)
    {
        BinaryFormatter objBF = new BinaryFormatter();
        System.IO.FileStream objFS = System.IO.File.Create(Server.MapPath("books.bin"));
        objBF.Serialize(objFS, bookslist);
        objBF = null;
        objFS.Close();
        objFS = null;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<Books> bookslist = GetBooksList();
        BinarySerialization(bookslist);
    }

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

    Public Function GetBooksList() As List(Of Books)
        Dim lstBooks As New List(Of Books) From { _
                                New Books With {.Title = "ASP.NET", .ISBN = "asp1", .ReleaseDate = DateTime.Parse("11/11/2010"), .Pages = 200, .PublisherId = 1}, _
                                New Books With {.Title = "C#.NET", .ISBN = "c#2", .ReleaseDate = DateTime.Parse("10/11/2010"), .Pages = 500, .PublisherId = 1}, _
                                New Books With {.Title = "VB.NET", .ISBN = "vb3", .ReleaseDate = DateTime.Parse("5/5/2009"), .Pages = 400, .PublisherId = 1}, _
                                New Books With {.Title = "SQL Server", .ISBN = "sql4", .ReleaseDate = DateTime.Parse("6/9/2010"), .Pages = 300, .PublisherId = 2}, _
                                New Books With {.Title = "JAVA", .ISBN = "java5", .ReleaseDate = DateTime.Parse("8/5/2011"), .Pages = 400, .PublisherId = 3}, _
                                New Books With {.Title = "HTML", .ISBN = "html6", .ReleaseDate = DateTime.Parse("9/5/2011"), .Pages = 400}}

        Return lstBooks
    End Function

    Private Sub BinarySerialization(ByVal bookslist As System.Collections.Generic.List(Of Books))
        Dim objBF As New BinaryFormatter()
        Dim objFS As System.IO.FileStream = System.IO.File.Create(Server.MapPath("books.bin"))
        objBF.Serialize(objFS, bookslist)
        objBF = Nothing
        objFS.Close()
        objFS = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim bookslist As System.Collections.Generic.List(Of Books) = GetBooksList()
        BinarySerialization(bookslist)
    End Sub

Output : 

(To view original image , click on image)


Click Here to view "Binary Deserialization with C# Examples and VB.Net Examples". Click Here...

This is very useful .Net Tips.

For Beginning .Net articles. 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