Home

SOAP Serialization

 

Serialization

The .NET Framework supports another technique of serialization referred to as SOAP (which stands for Simple Object Access Protocol). This technique a little more related to XML but, although we haven't studied XML, you don't need to know anything about it to use SOAP serialization.

To serialize an object using SOAP, you follow the same steps we reviewed for the binary serialization with one addition that you must add a certain reference.

When creating the class whose objects would be serialized, mark it with the [Serializable] attribute. Here is an example:

[Serializable]
public ref class CCar
{
public:
    String ^Make;
    String ^Model;
    int Year;
    int Color;
}

To support SOAP serialization, the .NET Framework provides the SoapFormatter class. This class is defined in the System::Runtime::Serialization::Formatters::Soap namespace that is part of the System::Runtime::Serialization::Formatters::Soap.dll assembly. In order to use The SoapFormatter class, you must reference this assembly. You can do this by adding it to your list of references:

Adding a reference to the System.Runtime.Serialization.Formatters.Soap.dll assembly

After these two steps, you can create an object and initialize it as you see fit. Before saving it, as always, create a Stream-based object that would indicate the name (and location) of the file and the type of action to perform. Then, declare a SoapFormatter variable using its default constructor. To actually save the object, call the Serialize() method of this class. This method uses the same syntax as that of the BinaryFormatter class: it takes two arguments. The first is a Stream-based object. The second is the object that needs to be serialized.

Here is an example:

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;

[Serializable]
public ref class CCar
{
public:
    String ^ Make;
    String ^ Model;
    int Year;
    int Color;
};

int main()
{
    CCar ^ vehicle = gcnew CCar;

    vehicle->Make  = L"Volvo";
    vehicle->Model = L"S40";
    vehicle->Year  = 2006;
    vehicle->Color = 3;

    FileStream ^ stmCar =
            gcnew FileStream(L"VolvoS40.car",
                           FileMode::Create);
    SoapFormatter ^ sopCar = gcnew SoapFormatter();

    sopCar->Serialize(stmCar, vehicle);
    return 0;
}
 

De-Serialization

De-serialization in soap is performed exactly as done for the binary de-serialization. To support it, the SoapFormatter class is equipped with the Deserialize() method. This method uses the same syntax as its equivalent of the BinaryFormatter class. The approach to use it is also the same.

Here is an example:

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;

[Serializable]
public ref class CCar
{
public:
    String ^ Make;
    String ^ Model;
    int Year;
    int Color;
};

int main()
{
    FileStream ^ stmCar =
            gcnew FileStream(L"VolvoS40.car",
                           FileMode::Open);
    SoapFormatter ^ sopCar = gcnew SoapFormatter();
    CCar ^ vehicle = dynamic_cast<CCar ^>(sopCar->Deserialize(stmCar));
        
    Console::WriteLine(L"Car Information");
    Console::WriteLine(L"Make:  {0}", vehicle->Make );
    Console::WriteLine(L"Model: {0}", vehicle->Model);
    Console::WriteLine(L"Year:  {0}", vehicle->Year);
    Console::Write(L"Color: ");
    switch(vehicle->Color)
    {
    case 1:
        Console::WriteLine(L"Black");
        break;
    case 2:
        Console::WriteLine(L"Gray");
        break;
    case 3:
        Console::WriteLine(L"White");
        break;
    case 4:
        Console::WriteLine(L"Red");
        break;
    case 5:
        Console::WriteLine(L"Blue");
        break;
    }
    return 0;
}

This would produce:

Car Information
Make:  Volvo
Model: S40
Year:  2006
Color: White
Press any key to continue . . .

 

 

Previous Copyright © 2007-2013, FunctionX Next