Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serialize a collection of objects

Serialize a collection of objects

Scheduled Pinned Locked Moved C#
csharpasp-netcomjsontutorial
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    godfetish
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • G godfetish

      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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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.”

      G 1 Reply Last reply
      0
      • H Henry Minute

        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.”

        G Offline
        G Offline
        godfetish
        wrote on last edited by
        #3

        Don't expect the help with ASP.NET, asked for help with serializing a List in C#, but thanks for reading the first sentance.

        H 1 Reply Last reply
        0
        • G godfetish

          Don't expect the help with ASP.NET, asked for help with serializing a List in C#, but thanks for reading the first sentance.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          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.”

          G 1 Reply Last reply
          0
          • H Henry Minute

            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.”

            G Offline
            G Offline
            godfetish
            wrote on last edited by
            #5

            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...

            H 1 Reply Last reply
            0
            • G godfetish

              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...

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              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: test

              BTW 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.”

              G 1 Reply Last reply
              0
              • H Henry Minute

                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: test

                BTW 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.”

                G Offline
                G Offline
                godfetish
                wrote on last edited by
                #7

                You are right! Thanks Henry. jrk

                H 1 Reply Last reply
                0
                • G godfetish

                  You are right! Thanks Henry. jrk

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  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.”

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups