Storing Objects into Permanant DataStore in ASP.Net

Hi All,

I was facing a problem towards sharing a data between Flash and .Net. The easiest way i found is XML Seralization because Remoting is much heavy and it requires remoting implementation. Even now I can store the Calss objects permanantely. This can be used in concept of Hibernate (Some what at very basic level).

I found XML Seralization because there must be some common bridge between two different technoloiges for communication. XML will act as common bridge between .Net and Flash.

XML Seralization can be done by System.Xml.Serialization.XmlSerializer class from System.Xml.Serialization namespace.

Assume our class defination looks like below –

namespace EShopping
{
public class Category
{
private int _CategoryId;
public string CatName;
public int CategoryId
{
get{return _CategoryId;}
set{_CategoryId=value;}
}
}
}

Our object has below contents-

EShopping.Category c = new EShopping.Category();
c.CategoryId = 1;
c.CatName = “Hello”;

Now we want to seralize this object to the XML by XML Seralization.

System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(EShopping.Category));

XmlSerializer Class has a method called Seralize and Deserialize.
Seralize method requires parameter Textwriter and class object to seralize.

System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(EShopping.Category));

System.IO.TextWriter tw = new System.IO.StreamWriter(Server.MapPath(“SeralizeClass.xml”));

ser.Serialize(tw, c);

tw.Close();

That’s it. Now you can see the SeralizeClass.xml file in your Web root directory. You can access this into any of the technologies which has xml support.

Accessing Seralize Object into .Net-

This is pretty straight forward and just access the object by Deserialize methotd of XmlSerializer Class.

System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(EShopping.Category));

System.IO.TextReader tr = new System.IO.StreamReader(Server.MapPath(“SeralizeClass.xml”));

EShopping.Category c = (EShopping.Category)ser.Deserialize(tr);

tr.Close();

Happy Coding 🙂