Wednesday, April 27, 2011

Serialize Code example

using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Serialization;
using CurrencyConvertor;
using System.Web.Services;
using SerializeObj;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
WebClass myClass = new WebClass();
TextBox1.Text = myClass.name = "Sheo Narayan";
TextBox2.Text = myClass.address = "Washington Dc,US";

string xmlObject = SerializeAnObject(myClass);
TextBox3.Text = xmlObject.Insert(5, "hello");
//DropDownList1.SelectedItem.Value = CurrencyConvertor.Currency.ZWD.ToString();
TextBox4.Text = CurrencyConvertor.Currency.ZMK.ToString();
WebClass deSerializedClass = (WebClass)DeSerializeAnObject(xmlObject);

string name = deSerializedClass.name;
string address = deSerializedClass.address;
TextBox6.Text = name;
TextBox7.Text = address;

}
///

/// Serialize an object

///


///
///

private string SerializeAnObject(object obj)
{

System.Xml.XmlDocument doc = new XmlDocument();

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

System.IO.MemoryStream stream = new System.IO.MemoryStream();

try
{

serializer.Serialize(stream, obj);

stream.Position = 0;

doc.Load(stream);

return doc.InnerXml;

}

catch
{

throw;

}

finally
{

stream.Close();

stream.Dispose();

}

}

///

/// DeSerialize an object

///


///
///

private object DeSerializeAnObject(string xmlOfAnObject)
{

WebClass myObject = new WebClass();

System.IO.StringReader read = new StringReader(xmlOfAnObject);

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());

System.Xml.XmlReader reader = new XmlTextReader(read);

try
{

myObject = (WebClass)serializer.Deserialize(reader);

return myObject;

}

//catch
//{

// throw;

//}

finally
{

reader.Close();

read.Close();

read.Dispose();

}

}

//protected void Button2_Click(object sender, EventArgs e)
//{

// WebClass deSerializedClass = (WebClass)DeSerializeAnObject();

// string name = deSerializedClass.name;
// string address = deSerializedClass.address;
//}
//protected void Button2_Click(object sender, EventArgs e)
//{
// WebClass myClassObject =new WebClass();




//}
protected void Button2_Click(object sender, EventArgs e)
{
WebClass Class = new WebClass();
string df;

df = Class.name.ToString();


Service gk = new Service();
TextBox8.Text = gk.SerializeAnObject(df = TextBox9.Text);

object dl = new object();

dl = DeSerializeAnObject(TextBox8.Text);
TextBox10.Text = dl.ToString();

//string name = deSerializedClass.name;
//string address = deSerializedClass.address;
//TextBox9.Text = name;
//TextBox10.Text = address;



}
public class Book
{
public string Title;
public string Isbn;
public string fbi;
}
//deserialize

protected void Button3_Click1(object sender, EventArgs e)
{
Book b = new Book();
b.Title = "Windows Forms";
b.Isbn = "728372837";
b.fbi = "521";

XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextWriter tw = new StreamWriter(Server.MapPath("Book1.xml"));
serializer.Serialize(tw, b);
tw.Close();
}
protected void Button4_Click1(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextReader tr = new StreamReader(Server.MapPath("Book1.xml"));
Book b = (Book)serializer.Deserialize(tr);
tr.Close();

Title.Text = b.Title;
Isbn.Text = b.Isbn;

}


protected void Button5_Click(object sender, EventArgs e)
{



XmlSerializer serializer = new XmlSerializer(typeof(Book));
TextReader tr = new StreamReader(Server.MapPath("book1.xml"));
Book b = (Book)serializer.Deserialize(tr);
tr.Close();
TextBox10.Text = tr.ToString();
Title.Text = b.Title;
Isbn.Text = b.Isbn;
}
[Serializable]
public class myTestClass
{
public string myString = "Hello World";
public int myInt = 1234;
public string[] myArray = new string[4];
private int myPrivateInt = 4321;

public string myMethod()
{
return "Hello World";
}
}
}

No comments:

Post a Comment