[Microsoft Techies Blog] How to Serialize and Deserialize in Soap Formatter c...

1 view
Skip to first unread message

Amit thakur

unread,
Aug 6, 2011, 12:19:18 AM8/6/11
to techie...@googlegroups.com
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;
}
}


--
Posted By Amit thakur to Microsoft Techies Blog at 8/05/2011 09:19:00 PM
Reply all
Reply to author
Forward
0 new messages