Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » How To » Downloads
 
Add your own
alternative version

Object Serialization using C#

, 31 Jan 2002
How to serialize custom created class objects using C#
objserial_demo.zip
ObjSerial.exe
objserial_src.zip
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace MyObjSerial
{
	[Serializable()]	//Set this attribute to all the classes that you define to be serialized
	public class Employee : 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
			EmpId = (int)info.GetValue("EmployeeId", typeof(int));
			EmpName = (String)info.GetValue("EmployeeName", typeof(string));
		}
		
		//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);
		}
	}
	
	//Main class
	public class ObjSerial
	{
		public static void Main(String[] args)
		{
			//Create a new Employee object
			Employee mp = new Employee();
			mp.EmpId = 10;
			mp.EmpName = "Omkumar";
			
			// Open a file and serialize the object into it in binary format.
			// EmployeeInfo.osl is the file that we are creating. 
			// Note:- you can give any extension you want for your file
			// If you use custom extensions, then the user will now 
			//   that the file is associated with your program.
			Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
			BinaryFormatter bformatter = new BinaryFormatter();
			
			Console.WriteLine("Writing Employee Information");
			bformatter.Serialize(stream, mp);
			stream.Close();
			
			//Clear mp for further usage.
			mp = null;
			
			//Open the file written above and read values from it.
			stream = File.Open("EmployeeInfo.osl", FileMode.Open);
			bformatter = new BinaryFormatter();
			
			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);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

omkamal
Web Developer
United States United States
Started programming with computers in 1995. Ever since it keeps me busy and creative. Did a bachelor's degree in electronics and communication only to better understand the inside of computers and its power. Currently working as a software developer in US and looking for a girl-friend...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Feb 2002
Article Copyright 2002 by omkamal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid