Using the binary reader and writer for psuedo-serialization : Class Serialization « Class « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Class » Class Serialization 
7.59.1.Using the binary reader and writer for psuedo-serialization
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;


struct Employee
{
    public string FirstName;
    public string LastName;
    public int Extension;
    public string SocialSecurityNumber;
    public bool Salaried;

    public Employee(string firstName, string lastName, int extension, string ssn, bool salaried)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Extension = extension;
        this.SocialSecurityNumber = ssn;
        this.Salaried = salaried;
    }

    public override string ToString()
    {
        return string.Format("{0}, {1}; {2}; {3}; {4}", LastName, FirstName, Extension, SocialSecurityNumber, Salaried);
    }
}

public class MainClass
{
    private static List<Employee> CreateEmployees()
    {
        List<Employee> emps = new List<Employee>();
        emps.Add(new Employee("J""D"3"001-21-2232"true));
        emps.Add(new Employee("G""B"5"001-21-0002"true));
        emps.Add(new Employee("J""R"9"322-21-4321"false));
        emps.Add(new Employee("B""B"3"331-22-1211"true));
        emps.Add(new Employee("H""S"9"991-28-2777"false));
        return emps;
    }

    private static List<Employee> DeserializeEmployees(Stream s)
    {
        List<Employee> employees = new List<Employee>();
        BinaryReader reader = new BinaryReader(s);

        try
        {
            while (true)
            {
                Employee e = new Employee();
                e.FirstName = reader.ReadString();
                e.LastName = reader.ReadString();
                e.Extension = reader.ReadInt32();
                e.SocialSecurityNumber = reader.ReadString();
                e.Salaried = reader.ReadBoolean();
                employees.Add(e);
                Console.WriteLine("Read: {0}", e.ToString());
            }
        }
        catch (EndOfStreamException)
        {
        }

        return employees;
    }

    private static void SerializeEmployees(Stream s, IEnumerable<Employee> employees)
    {
        BinaryWriter writer = new BinaryWriter(s);
        foreach (Employee e in employees)
        {
            writer.Write(e.FirstName);
            writer.Write(e.LastName);
            writer.Write(e.Extension);
            writer.Write(e.SocialSecurityNumber);
            writer.Write(e.Salaried);
            Console.WriteLine("Wrote: {0}", e.ToString());
        }
    }

    public static void Main()
    {
        Stream s = new MemoryStream();
        IEnumerable<Employee> employees = CreateEmployees();

        SerializeEmployees(s, employees);

        s.Seek(0, SeekOrigin.Begin);
        DeserializeEmployees(s);

        s.Seek(0, SeekOrigin.Begin);
        int read;
        while ((read = s.ReadByte()) != -1)
            Console.Write("{0:X} ", read);

    }
}
Wrote: D, J; 3; 001-21-2232; True
Wrote: B, G; 5; 001-21-0002; True
Wrote: R, J; 9; 322-21-4321; False
Wrote: B, B; 3; 331-22-1211; True
Wrote: S, H; 9; 991-28-2777; False
Read: D, J; 3; 001-21-2232; True
Read: B, G; 5; 001-21-0002; True
Read: R, J; 9; 322-21-4321; False
Read: B, B; 3; 331-22-1211; True
Read: S, H; 9; 991-28-2777; False
1 4A 1 44 3 0 0 0 B 30 30 31 2D 32 31 2D 32 32 33 32 1 1 47 1 42 5 0 0 0 B 30 30 31 2D 32 31 2D 30 3
0 30 32 1 1 4A 1 52 9 0 0 0 B 33 32 32 2D 32 31 2D 34 33 32 31 0 1 42 1 42 3 0 0 0 B 33 33 31 2D 32
32 2D 31 32 31 31 1 1 48 1 53 9 0 0 0 B 39 39 31 2D 32 38 2D 32 37 37 37 0
7.59.Class Serialization
7.59.1.Using the binary reader and writer for psuedo-serialization
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.