Showing posts with label System.Runtime.Serialization. Show all posts
Showing posts with label System.Runtime.Serialization. Show all posts

Wednesday, January 30, 2008

soap formatter to serialize objects in C#

Guys, we have already seen that how can we serialize an object using C# in visual studio 2005. Now, we'll look into the process of serializing an object using soap formatter. It is also known as xml serialization. In this article we'll get into the details of serializing an object using soap formatter.

First of all, import following two namespaces in your project.

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

We'll use the same example of the Product class, which we used in object serializatin using binary formatter.

[Serializable]
class Product{
public int ProductID;
public double Price;
public string Name;

public Product(int _productID, double _price, string _name)
{
ProductID = _productID;
Price = _price;
Name = _name;
}
}

For basic understanding of object serialization you can view following post

Object serialization using binary formatters in C#

Following code serializes an object using soap formatter:

Product objProduct = new Product(2,500,'some product');
FileStream fs = new FileStream("c:\\productobjectsoapformatted.Data", FileMode.Create);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(fs, objProduct);
fs.Close();

To deserialize you'll have to use the following code:

Product objProduct;
FileStream fs = new FileStream("c:\\productobjectsoapformatted.Data", FileMode.Open);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(fs, objProduct);
fs.Close();

If you open this data file in some text editor you'll see following output:


<?xml:namespace prefix = soap-env /><soap-env:envelope encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" env="http://schemas.xmlsoap.org/soap/envelope/" enc="http://schemas.xmlsoap.org/soap/encoding/" xsd="http://www.w3.org/2001/XMLSchema" xsi="http://www.w3.org/2001/XMLSchema-instance"><soap-env:envelope xsi="http://www.w3.org/2001/XMLSchema-instance" xsd="http://www.w3.org/2001/XMLSchema" enc="http://schemas.xmlsoap.org/soap/encoding/" env="http://schemas.xmlsoap.org/soap/envelope/" clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap-env:body<
<a1:shoppingcartitem id="ref-1" a1="http://schemas.microsoft.com/clr/nsassem/Serialization/Serialization%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<productid>1</productid>
<price>5</price>
<items>3</items>
<total>15</total>
</a1:ShoppingCartItem>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</soap-env:body>
</soap-env:envelope>

Saturday, January 26, 2008

Object serialization using binary formatters in C#

In this article I'll show you the method of serializing a class object on a disk drive in a data file using binary formatters in C#. When we serialize an object, we actually preserve the state of the object on the disk so we can pick its state right from there where we left.

A classic example can be of a video game. For example you're developing a video game and you want to stop and exit the game but you would also like to start playing the game from where you left it. You could achieve this functionality as a game developer to serialize all of your objects on the disk.

We have already seen how we can serialize and deserialize some text using binary formatter in Microsoft.Net in a previous article:
Serialization of data using binary formatters in C#

In this article we're using binary formatter again for serialization but this time we'll serialize an object. You'll need following two namespaces for this purpose.

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

First of all we need to define a class which is serializable:

[Serializable]
class Product
{
public int ProductID;
public double Price;
public string Name;


public Product(int _productID, double _price, string _name)
{
ProductID = _productID;
Price = _price;
Name = _name;
}
}

This is a simple product class having attribute Serializable, which makes objects of this class capable of being serialized.

Following code serializes the object of class product on the disk.

Product objProduct = new Product(1,200,'someproduct');
FileStream fs = new FileStream("c:\\productobject.Data", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, objProduct);
fs.Close();

You'll see that when you execute this code it'll create a file with the specified name on the hard drive and will save the object's status in it.

Now, when you want to deserialize the object, that is, you want to load the state of the object in to an object of Product class you'll use following code.

Product objProduct;
FileStream fs = new FileStream("c:\\productobject.Data", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
objProduct = (Product)bf.Deserialize(fs);
fs.Close();

In some next article I'll show you how can we serialize an object using Soap formatter that is also known as XML serialization.