المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Serialize a collection of objects



C# Programming
08-26-2009, 08:05 PM
In C# I need to serialize a List of Constraint objects. Each Constraint has a Group, Field, Condition, and some values depending on the CGF combination. I have been trying to get these serialized so that I can store them, preferably in an ASP.NET ******. I cannot use Session to maintain the list because I was told we have 2 web servers and Session won't work, so I need to serialize the CList and push it off in a ******...I'm new to ASP.NET, but I thought skilled in C# and started working on a test app to serialize a List of generic objects based on a sample app here but it is not working properly.

In this example I make a List and add to it a few employees, then try to serialize it and store off in a file. Reset my List and such, then open the file a read the serialized data back to the List.

When using the original project, one employee at a time worked. The original is here: http://www.codeproject.com/KB/cs/objserial.aspx

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;

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
List mp = new List();
Employee e = new Employee();
e.EmpId = 10;
e.EmpName = "Omkumar";
mp.Add(e);
e.EmpId = 11;
e.EmpName = "this";
mp.Add(e);
e.EmpId = 12;
e.EmpName = "is a";
mp.Add(e);
e.EmpId = 13;
e.EmpName = "test";
mp.Add(e);

mp.Add(e);

// 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 ostream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

Console.WriteLine("Writing Employee Information");
bformatter.Serialize(ostream, mp);
ostream.Close();

//Clear mp and bf for further usage.
mp = null;
bformatter = null;

//Open the file written above and read values from it.
Stream istream = File.Open("EmployeeInfo.osl", FileMode.Open);
bformatter = new BinaryFormatter();

Console.WriteLine("Reading Employee Information");
mp = (List)bformatter.Deserialize(istream);
istream.Close();
foreach (Employee f in mp)
{
Console.WriteLine("Employee Id: {0}", f.EmpId.ToString());
Console.WriteLine("Employee Name: {0}", f.EmpName);
}

}
}
}


Thanks for any help you might give.

jrk