Use SoapFormatter to do Soap Serialize in CSharp

Description

The following code shows how to use SoapFormatter to do Soap Serialize.

Example


/* ww  w .j  a v a 2  s . co m*/
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

class MainClass
{
    private static void BinarySerialize(ArrayList list)
    {
        using (FileStream str = File.Create("people.bin"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(str, list);
        }
    }

    private static ArrayList BinaryDeserialize()
    {
        ArrayList people = null;
        using (FileStream str = File.OpenRead("people.bin"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            people = (ArrayList)bf.Deserialize(str);
        }
        return people;
    }

    private static void SoapSerialize(ArrayList list)
    {
        using (FileStream str = File.Create("people.soap"))
        {
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(str, list);
        }
    }

    private static ArrayList SoapDeserialize()
    {
        ArrayList people = null;
        using (FileStream str = File.OpenRead("people.soap"))
        {
            SoapFormatter sf = new SoapFormatter(); 
            people = (ArrayList)sf.Deserialize(str);
        }
        return people;
    }
    public static void Main()
    {

        ArrayList people = new ArrayList();
        people.Add("G");
        people.Add("L");
        people.Add("A");

        BinarySerialize(people);
        SoapSerialize(people);

        ArrayList binaryPeople = BinaryDeserialize();
        ArrayList soapPeople = SoapDeserialize();
        Console.WriteLine("Binary people:");
        foreach (string s in binaryPeople)
        {
            Console.WriteLine("\t" + s);
        }
        Console.WriteLine("\nSOAP people:");
        foreach (string s in soapPeople)
        {
            Console.WriteLine("\t" + s);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip