Serialize a collection of objects
-
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 cookie. 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 cookie...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<Employee> mp = new List<Employe
-
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 cookie. 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 cookie...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<Employee> mp = new List<Employe
godfetish wrote:
.I'm new to ASP.NET
And I know zilch about it. I'll bet there are a lot of people over in the ASP.NET Forum that do though.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
godfetish wrote:
.I'm new to ASP.NET
And I know zilch about it. I'll bet there are a lot of people over in the ASP.NET Forum that do though.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Don't expect the help with ASP.NET, asked for help with serializing a List in C#, but thanks for reading the first sentance.
Sorry about that. :-O My eyes are tired, that's my excuse, and I'm sticking to it. solution:
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 = new Employee(); //<==============================
e.EmpId = 11;
e.EmpName = "this";
mp.Add(e);
e = new Employee(); //<==============================
e.EmpId = 12;
e.EmpName = "is a";
mp.Add(e);
e = new Employee(); //<==============================
e.EmpId = 13;
e.EmpName = "test";
mp.Add(e);mp.Add(e);
You do realise you will get two 13 employees, because of the last line.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Sorry about that. :-O My eyes are tired, that's my excuse, and I'm sticking to it. solution:
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 = new Employee(); //<==============================
e.EmpId = 11;
e.EmpName = "this";
mp.Add(e);
e = new Employee(); //<==============================
e.EmpId = 12;
e.EmpName = "is a";
mp.Add(e);
e = new Employee(); //<==============================
e.EmpId = 13;
e.EmpName = "test";
mp.Add(e);mp.Add(e);
You do realise you will get two 13 employees, because of the last line.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
The output of the sample project is:
Writing Employee Information Reading Employee Information Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Press any key to continue . . .
So, yes, two EId's of 13 should be there, by design or by fault...not 5 of them when deserialized. I see that the de/serialization is only calling the get/set in the class object one time each way. Not 5 times, once for each record! This is an interesting problem... -
The output of the sample project is:
Writing Employee Information Reading Employee Information Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Employee Id: 13 Employee Name: test Press any key to continue . . .
So, yes, two EId's of 13 should be there, by design or by fault...not 5 of them when deserialized. I see that the de/serialization is only calling the get/set in the class object one time each way. Not 5 times, once for each record! This is an interesting problem...I copied your code to a new Console Application and ran it. I got the result that you have just posted. Then I made the modifications, as in my previous post, and I got:
Writing Employee Information
Reading Employee Information
Employee Id: 10
Employee Name: Omkumar
Employee Id: 11
Employee Name: this
Employee Id: 12
Employee Name: is a
Employee Id: 13
Employee Name: test
Employee Id: 13
Employee Name: testBTW When you post code snippets, if you are currently highlighting your code and then clicking on the 'inline code' widget (immediately above the text entry box) would you please use the 'code block' widget next to it. (explanation follows) If on the other hand, you are typing in your 'code' tags could you please use 'pre' in place of 'code'. EXPLANATION: The 'code' tags give the horrible red and blue code colouration, which is very hard on the eyes and is one of the reasons I didn't read all of your original post. I'm being serious here, it actually makes my eyes hurt, and I know that applies to other CP members as well. The 'pre' tags give much more restful colouring and what's more it preserves the formatting (well mostly) of your code, making it much easier to read.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I copied your code to a new Console Application and ran it. I got the result that you have just posted. Then I made the modifications, as in my previous post, and I got:
Writing Employee Information
Reading Employee Information
Employee Id: 10
Employee Name: Omkumar
Employee Id: 11
Employee Name: this
Employee Id: 12
Employee Name: is a
Employee Id: 13
Employee Name: test
Employee Id: 13
Employee Name: testBTW When you post code snippets, if you are currently highlighting your code and then clicking on the 'inline code' widget (immediately above the text entry box) would you please use the 'code block' widget next to it. (explanation follows) If on the other hand, you are typing in your 'code' tags could you please use 'pre' in place of 'code'. EXPLANATION: The 'code' tags give the horrible red and blue code colouration, which is very hard on the eyes and is one of the reasons I didn't read all of your original post. I'm being serious here, it actually makes my eyes hurt, and I know that applies to other CP members as well. The 'pre' tags give much more restful colouring and what's more it preserves the formatting (well mostly) of your code, making it much easier to read.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Pleasure! Sorry again, for the misunderstanding.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”