/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
namespace MyObjSerial { [Serializable()] //Set this attribute to all the classes that want to serialize public class Employee : ISerializable //derive your class from ISerializable { public int EmpId; public string EmpName; //Default constructor public Employee() { EmpId = 0; EmpName = null; } } //Deserialization constructor. public Employee(SerializationInfo info, StreamingContext ctxt) { //Get the values from info and assign them to the appropriate properties } //Serialization function. public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { //You can use any custom name for your name-value pair. But make sure you // read the values with the same name. For ex:- If you write EmpId as "EmployeeId" // then you should read the same with "EmployeeId" info.AddValue("EmployeeId", EmpId); info.AddValue("EmployeeName", EmpName); } } // Serialize Sample Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create); Console.WriteLine("Writing Employee Information"); bformatter.Serialize(stream, mp); stream.Close(); // Deserialize Sample mp = null; //Open the file written above and read values from it. stream = File.Open("EmployeeInfo.osl", FileMode.Open); Console.WriteLine("Reading Employee Information"); mp = (Employee)bformatter.Deserialize(stream); stream.Close(); Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString()); Console.WriteLine("Employee Name: {0}",mp.EmpName);
URL: http://www.codeproject.com/csharp/objserial.asp