Deserialize : Serialization « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » SerializationScreenshots 
Deserialize
 

using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class Employee : ISerializable {
    private string name;
    private int age;
    private string address;

    public Employee(string name, int age, string address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    private Employee(SerializationInfo info, StreamingContext context) {
        name = info.GetString("Name");
        age = info.GetInt32("Age");
        try {
            address = info.GetString("Address");
        catch (SerializationException) {
            address = null;
        }
    }

    public string Name {
        get return name; }
        set name = value; }
    }

    public int Age {
        get return age; }
        set age = value; }
    }

    public string Address {
        get return address; }
        set address = value; }
    }

    public void GetObjectData(SerializationInfo inf, StreamingContext con) {
        inf.AddValue("Name", name);
        inf.AddValue("Age", age);

        if ((con.State & StreamingContextStates.File== 0) {
            inf.AddValue("Address", address);
        }
    }

    public override string ToString() {
        StringBuilder str = new StringBuilder();

        str.AppendFormat("Name: {0}\r\n", Name);
        str.AppendFormat("Age: {0}\r\n", Age);
        str.AppendFormat("Address: {0}\r\n", Address);

        return str.ToString();
    }
}

public class MainClass {
    public static void Main(string[] args) {
        Employee roger = new Employee("R"56"L");
        Console.WriteLine(roger);
        Stream str = File.Create("r.bin");
        BinaryFormatter bf = new BinaryFormatter();
        bf.Context = new StreamingContext(StreamingContextStates.CrossAppDomain);
        bf.Serialize(str, roger);
        str.Close();

        str = File.OpenRead("r.bin");
        bf = new BinaryFormatter();
        roger = (Employee)bf.Deserialize(str);
        str.Close();
        Console.WriteLine(roger);
        str = File.Create("r.bin");
        bf = new BinaryFormatter();
        bf.Context = new StreamingContext(StreamingContextStates.File);
        bf.Serialize(str, roger);
        str.Close();

        str = File.OpenRead("r.bin");
        bf = new BinaryFormatter();
        roger = (Employee)bf.Deserialize(str);
        str.Close();
        Console.WriteLine(roger);

    }
}

           
         
  
Related examples in the same category
1.Use Serializable attribute to mark a generic class
2.Use Serializable attribute to mark a class
3.Deserialize Object
4.Serialize and DeSerialize
5.Three types of Serialization: Binary, Soap, XML
6.Use XML Serialization with Custom ObjectsUse XML Serialization with Custom Objects
7.Working with the Serializable Attribute
8.NonSerialized attributesNonSerialized attributes
9.Serialize hiearchy classesSerialize hiearchy classes
10.C# Serialization C# Serialization
11.Serial Employee class
12.Set XML tag name for Serialization
13.illustrates binary serializationillustrates binary serialization
14.Collection Serialization
15.Serializes an object to binary
16.Deserializes an object from binary
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.