0% found this document useful (0 votes)
40 views3 pages

Soap Serialization Example in C#

The document discusses serialization and deserialization using SOAP and XML formats. It provides code examples of serializing an employee class to a file using SOAP serialization, and deserializing it. It also shows serializing and deserializing a Book class using XML serialization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Soap Serialization Example in C#

The document discusses serialization and deserialization using SOAP and XML formats. It provides code examples of serializing an employee class to a file using SOAP serialization, and deserializing it. It also shows serializing and deserializing a Book class using XML serialization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Soap Serialization and Deserialization:

Example:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace soap_seri
{
[Serializable] class employee
{
int eid;
string ename;
public employee(int eid,string ename)
{
this.eid = eid;
this.ename = ename;
}
static void Main(string[] args)
{
employee e = new employee(101, "sara");
string path = @"S:\\soap1.txt";
FileStream st = new FileStream(path, FileMode.Create);
SoapFormatter sf=new SoapFormatter();
sf.Serialize(st, e);
st.Close();
//Soap Deserialization
string path1 = @"S:\\soap1.txt";
FileStream st1 = new FileStream(path1, FileMode.Open);
SoapFormatter sf1 = new SoapFormatter();
employee eee = (employee)sf1.Deserialize(st1);
Console.WriteLine("Emp id is:"+eee.eid);
Console.WriteLine("emp name is:"+eee.ename);
st1.Close();
Console.ReadKey();
}
}
}

Soap1.txt

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-
ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-
ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>

<a1:employee id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/soap_seri/soap_seri


%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">

<eid>101</eid>
<ename id="ref-3">sara</ename>

</a1:employee>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Output:

Emp id is:101

emp name is:sara

Xml Serialization and Deserialization

Program:

using System;
using System.IO;
using System.Xml.Serialization;
namespace xml_seri
{
public class Book
{
int _aid;
string _author;
public int aid
{
get
{
return _aid;
}
set
{
_aid = value;
}
}
public string author
{
get
{
return _author;
}
set
{
_author = value;
}
}
static void Main(string[] args)
{
//Xml Serialization
Book bo = new Book();
bo.aid = 101;
bo.author = "sara";
string path = @"S:\\xml_file.xml";
FileStream stream = new FileStream(path,FileMode.Create,FileAccess.Write);
XmlSerializer xs = new XmlSerializer(typeof(Book));
xs.Serialize(stream, bo);
//XmlSerializer Deserialization
stream.Close();
string path1 = @"S:\\xml_file.xml";
FileStream stream1 = new FileStream(path1, FileMode.Open);
XmlSerializer xs1 = new XmlSerializer(typeof(Book));
Book b2=(Book)xs1.Deserialize(stream1);
Console.WriteLine("Author id is:" + b2.aid);
Console.WriteLine("Author name is:"+b2._author);

Console.ReadKey();

}
}
}

Xml_file.xml

<?xml version="1.0"?>

<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<aid>101</aid>

<author>sara</author>

</Book>

Output:

Author id is:101

Author name is:sara

You might also like