Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Serialization
Author: phanindra yerra
Published: 6/23/2006 5:38 AM
Category: C#
Summary: In this article I explained what serialization is and how to do serialize an object into xml format in next article I will explain how to serialize object in to binary and soap formats

 

Serialization

 

System.Runtime.Serialization namespace

 

 

 What is Serialization?

Serialization is the process of converting an object in to some format , which is comfortable to transport  across a network or persisted to a storage location. The serialized format contains the object's state information.

 What is Deserialization?

 Deserialization is the process of using the serialized state information to reconstruct the object from the    serialized state to its original state.

 Why we need serialization and deserialization

1)      Easy To Transport  : we can  easily transport data of type text or binary across the network rather than transporting an object

2)      Platform Independent: suppose for example you serialize a .net object to xml format and send it to any where. Any language can understand xml format.   

   Serialization   in .Net

 

                        Microsoft .NET framework provides 3 formats to which objects can be serialized. The formats are binary, SOAP, and XML. The format is controlled based upon what object is used to perform the serialization.

.Net Namespaces and classes for serialization

The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

Out of all the 3 formats the binary format is the most compact and light of the three formats. The XML formatter only serializes public fields and properties, while binary and SOAP do not adhere to that limitation.

  Example

 

         using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Xml.Serialization;

using System.IO; 

namespace WorkingProject

{

      /// <summary>

      /// Summary description for Serialization.

      /// </summary>

      //////// this tag is for defining root tag name

            [XmlRoot("school")]

        public class School

            {

                  // for defining atttibute

                  [XmlAttribute("SchoolName")]

                  public string SchoolName = "stmarys";

                  private string  _rno ;

                  private string _name = "";

            private string _branch = "";

             // for defining an element

            [XmlElement("StudentName")]

                  public string Name

                  {

                        get { return this._name; }

                        set { this._name = value; }

                  }

                  public string Number

                  {

                        get { return this._rno; }

                        set { this._rno = value; }

                  }

              // igonring this while serializing

                [XmlIgnore]

                  public string Branch

                  {

                        get { return this._branch; }

                        set { this._branch = value; }

                  }

 

                  public School()

                  {

                        _name = "phani";

                        _rno = "01e41a0520";

                        _branch = "cse";

                             

                  }

           

            }

 

     

     

     

      public class Serialization : System.Web.UI.Page

      {

            private void Page_Load(object sender, System.EventArgs e)

            {

                  // Put user code to initialize the page here

 

                  School s1 = new School();

            s1.Name = "phanindra";

             

                  XmlSerializer serializer = new XmlSerializer(s1.GetType());

                  Stream stream = new FileStream("e:\\serialized.xml", FileMode.Create,

                        FileAccess.ReadWrite, FileShare.None);

                 

                  serializer.Serialize(stream, s1);

                 

                  stream.Close();

 

 

            }

 

Output Xml

 

         <?xml version="1.0" ?>

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

  <StudentName>phanindra</StudentName>

  <Number>01e41a0520</Number>

  </school>

 

Summary

 

         In this article I explained what serialization is and how to do serialize an object into xml format in next article I will explain how to serialize object in to binary and soap formats

 

Thanks for reading .


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
remote online storage
Color Laser Printer
Marc jacobs sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139