Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » .NET Remoting » .Net Serialization using Soap Formatter

.Net Serialization using Soap Formatter

This article explains how to serialize objects using soap formatter which is used extensively in .Net Remoting.

Page Views : 14656
Downloads : 170
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
serialization and deserialization.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Serialization is method to persist the state of an object in order to have the ability to recreate the same object when required. when the object is required to travel electronically over the network, in such cases the objects are serialized at one end and deserialized at the other end. serialization is one of the fundamental requirements for techniques such as .NET Remoting.

Serialization in .NET are of four types as shown in:

 

  1. XML serialization allows you to serialize objects into XML data format. However it can convert only public properties of an object into xml data. Therefore it is referred to as shallow serialization.
  2. In SOAP and BINARY serialization technique the state of the entire object is serialized into a stream of bytes. In cases where the object contains a reference to other objects, even those are serialized. This type of serialization is known as deep serialization.This overcomes the disadvantage of XML serializer to serialize the private fields of an object.

The .NET Framework also provides the SoapFormatter and the BinaryFormatter classes. These classes implement the IRemotingFormatter and the IFormatter interfaces.

The main methods which encapsulate the functionality of serialization are the Serialize and Deserialize methods.

Here we study the soap formatter�..

[Serializable]

public class secreteClass

{

private int myaccountnumber = 0;

[NonSerialized()]

private string password = "qwerty321";

 

public int MyAccNum

{

get

{

return myaccountnumber;

}

set

{

myaccountnumber = value;

}

}

public string Passwd

{

get

{

return password;

}

set

{

password = value;

}

}

public secreteClass()

{

 

}

}

Note: Here password is not serialized and hence it is shown as �[NonSerialized()]�.

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

Public static void Main()

{

try

{

 

secreteClass objSecrete = new secreteClass();

objSecrete.MyAccNum = 123321456;

 

//Soap serialization technique

SoapFormatter formatter = new SoapFormatter();

Stream objfilestream = new FileStream("c:\\Myserialzed.xml", FileMode.Create, FileAccess.Write, FileShare.None);

 

formatter.Serialize(objfilestream, objSecrete);

objfilestream.Close();

//deserialization

Stream objreadstream = new FileStream("c:\\Myserialzed.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

secreteClass objSecrete2 = (secreteClass)formatter.Deserialize(objreadstream);

 

int Myaccno = objSecrete2.MyAccNum;

Console.Writeln("AccNo:{0} " + Myaccno.ToString());

}

catch (Exception ex)

{

}

}

Note: When building this code, you must reference the System.Runtime.Serialization.Formatters.Soap.dll assembly.

This is the XML file(Myserialzed.xml) screenshot, where the data is serialized and stored.

Myserialized.xml

<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:serderlize_x002B_secreteClass id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/assem/App_Web_1imlsxqk%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">

<myaccountnumber>123321456</myaccountnumber>

</a1:serderlize_x002B_secreteClass>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

In the next article will see other types of serialization techniques.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by SubbaramiReddy Y on Jul 06, 2010

//i have an error while serializing PVPolicy class,below is the my code
//The error is error in generating Xml textdeocument


 String XmlizedString = null;
                        MemoryStream memoryStream = new MemoryStream();
                        XmlSerializer xs = new XmlSerializer(pvPolicy.GetType());
                        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                        xs.Serialize(xmlTextWriter, pvPolicy);
                        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
 [Top] Rate this article
 
 About the author
 
Sudheendra Desai
C#, ASP.Net, SQL Server, Javascript, XML, CSS, Ajax,PHP,Design patterns
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.