Friday, August 5, 2011

How to Serialize and Deserialize in Soap Formatter class:

Soap Formatter class is located in the System.Runtime.Serialization.Formatters.Soap namespace. You can use this class to serialize data in SOAP format. The data that the Binary Formatter class emits is compact, but it is not easily readable by applications that are built by using other technologies. The data that the Soap Formatter class generates is more verbose, but it follows a standard format that other platforms can read from and write to.

Example of Soap Formatter:

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

namespace mod6_SoapSerlization
{
class Program
{
static void Main(string[] args)
{
myclass m = new myclass();
Console.WriteLine("Enter the Course name:");
m.Cname = Console.ReadLine();
Console.WriteLine("Enter the Course Fee:");
m.Cfee = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Course Durations:");
m.Durations = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(m.Cname);
Console.WriteLine(m.Cfee);
Console.WriteLine(m.Durations);

////Soap Serialization
//using (FileStream filestream = new FileStream(@"D:\\binfile1.txt", FileMode.OpenOrCreate, FileAccess.Write))
//{
// SoapFormatter formatter = new SoapFormatter();

// formatter.Serialize(filestream, m);

// filestream.Close();
//}
//m.Cname = "CoreJava";
m.Cfee = 18000;
m.Durations = 6;


//Deserialization
using (FileStream filestream = new FileStream(@"D:\\binfile1.txt", FileMode.Open, FileAccess.Read))
{
myclass m1;
SoapFormatter formatter = new SoapFormatter();
m1 = (myclass)formatter.Deserialize(filestream);
filestream.Close();
Console.WriteLine(m.Cname);
Console.WriteLine(m.Cfee);
Console.WriteLine(m.Durations);
Console.ReadLine();
}



}
}

[Serializable ]
public class myclass
{
public string Cname;
public int Cfee;
public int Durations;
}
}

No comments:

Recent Posts