C# Serialization : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » SerializationScreenshots 
C# Serialization
C# Serialization
    
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 32 - .NET Frameworks Overview\Serialization
// copyright 2000 Eric Gunnerson
// file=serial.cs
// compile with: csc serial.cs
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

public class FrameworksOverviewSerialization
{
    public static void Main()
    {
        MyRow row = new MyRow();
        row.Add(new MyElement("Gumby"));
        row.Add(new MyElement("Pokey"));
        
        Console.WriteLine("Initial value");
        Console.WriteLine("{0}", row);
        
        // write to binary, read it back
        Stream streamWrite = File.Create("MyRow.bin");
        BinaryFormatter binaryWrite = new BinaryFormatter();
        binaryWrite.Serialize(streamWrite, row);
        streamWrite.Close();
        
        Stream streamRead = File.OpenRead("MyRow.bin");
        BinaryFormatter binaryRead = new BinaryFormatter();
        MyRow rowBinary = (MyRowbinaryRead.Deserialize(streamRead);
        streamRead.Close();
        
        Console.WriteLine("Values after binary serialization");
        Console.WriteLine("{0}", rowBinary);
        
        // write to SOAP (XML), read it back
        streamWrite = File.Create("MyRow.xml");
        SoapFormatter soapWrite = new SoapFormatter();
        soapWrite.Serialize(streamWrite, row);
        streamWrite.Close();
        
        streamRead = File.OpenRead("MyRow.xml");
        SoapFormatter soapRead = new SoapFormatter();
        MyRow rowSoap = (MyRowsoapRead.Deserialize(streamRead);
        streamRead.Close();
        
        Console.WriteLine("Values after SOAP serialization");
        Console.WriteLine("{0}", rowSoap);
    }
}

[Serializable]
public class MyElement
{
    public MyElement(string name)
    {
        this.name = name;
        this.cacheValue = 15;
    }
    public override string ToString()
    {
        return(String.Format("{0}: {1}", name, cacheValue));
    }
    string name;
    // this field isn't persisted.
    [NonSerialized]
    int cacheValue;
}
[Serializable]
public class MyRow
{
    public void Add(MyElement my)
    {
        row.Add(my);
    }
    
    public override string ToString()
    {
        string temp = null;
        foreach (MyElement my in row)
        temp += my.ToString() "\n"
        return(temp);
    }
    
    ArrayList row = new ArrayList();    
}



           
         
    
    
    
  
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.Serial Employee class
11.Set XML tag name for Serialization
12.illustrates binary serializationillustrates binary serialization
13.Deserialize
14.Collection Serialization
15.Serializes an object to binary
16.Deserializes an object from binary
17.Object to string serialization/Deserialization
18.Object to byte array serialization/Deserialization
19.Javascript Serializer
20.Serialize and Deserialize (2)
21.Binary Serializer
22.Deserialize the incomming data to the T datatype, this method used to deserialize the test cases data to the required entity
23.Serialization Utility
24.Serialization Utilities
25.Clone With Serialization
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.