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. Serialization of arrays

Serialization of arrays

Scheduled Pinned Locked Moved C#
xmlwcfdata-structuresdebuggingjson
6 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.
  • R Offline
    R Offline
    Rudolf Jan
    wrote on last edited by
    #1

    I want to serialize an array of objects, using the SOAP formatter. However, when serializing the second object, I get an exception that says I cannot add the same object twice to a serialization info. I wrote a small application to demonstrate the problem:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.Serialization;
    using System.Xml;
    using System.Runtime.Serialization.Formatters.Soap;

    namespace Serialization
    {
    // Create a class to include in the list
    [Serializable]
    public class MyObject:ISerializable
    {
    public Int32 idx=0;

    	public MyObject(Int32 i)
    		{
    		idx=i;
    		}
    
    	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
    		{
    		info.AddValue("Idx",idx);
    		}
    	}
    
    // The objectlist consists of a list of MyObjects
    \[Serializable\]
    class ObjectList: ISerializable
    	{
    	public List TheList;
    
    	public ObjectList()
    		{
    		TheList=new List(10);
    		MyObject tmp=new MyObject(1);
    		TheList.Add(tmp);
    		tmp=new MyObject(2);
    		TheList.Add(tmp);
    		}
    
    	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
    		{
    		Int32 i=0;
    
    		for(i=0;i<2;i++)
    			{
    			if(TheList\[i\]!=null)
    				{
    				// Serialize a MyObject contained in TheList
    				// This does not work for the second occurrence of MyObject
    				TheList\[i\].GetObjectData(info,context);
    				}
    			}
    		}
    	}
    
    class Program
    	{
    	static void Main(string\[\] args)
    		{
    		ObjectList MyObjectList=new ObjectList();
    		FileStream WriteStream=new FileStream("test.xml",FileMode.Create); 
    		SoapFormatter bfor=new SoapFormatter();
    		bfor.Serialize(WriteStream,MyObjectList);
    		}
    	}
    }
    

    In the sample I create a class MyObject. Objects of type MyObject are stored in an ObjectList object, which essentially is a wrapper around a LIST generic List object. I have no idea what I'm doing wrong. I did not find any samples on how to handle array serialization. Please note that the application I develop is far more complex than this. Therefore I do not want to use automatic serialization. I use the SOAP formatter because it is easier to debug. Later I will switch to the binary formatter. Tanks in advance for your advice. Rudolf Heijink

    L 1 Reply Last reply
    0
    • R Rudolf Jan

      I want to serialize an array of objects, using the SOAP formatter. However, when serializing the second object, I get an exception that says I cannot add the same object twice to a serialization info. I wrote a small application to demonstrate the problem:

      using System;
      using System.IO;
      using System.Collections.Generic;
      using System.Text;
      using System.Runtime.Serialization;
      using System.Xml;
      using System.Runtime.Serialization.Formatters.Soap;

      namespace Serialization
      {
      // Create a class to include in the list
      [Serializable]
      public class MyObject:ISerializable
      {
      public Int32 idx=0;

      	public MyObject(Int32 i)
      		{
      		idx=i;
      		}
      
      	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
      		{
      		info.AddValue("Idx",idx);
      		}
      	}
      
      // The objectlist consists of a list of MyObjects
      \[Serializable\]
      class ObjectList: ISerializable
      	{
      	public List TheList;
      
      	public ObjectList()
      		{
      		TheList=new List(10);
      		MyObject tmp=new MyObject(1);
      		TheList.Add(tmp);
      		tmp=new MyObject(2);
      		TheList.Add(tmp);
      		}
      
      	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
      		{
      		Int32 i=0;
      
      		for(i=0;i<2;i++)
      			{
      			if(TheList\[i\]!=null)
      				{
      				// Serialize a MyObject contained in TheList
      				// This does not work for the second occurrence of MyObject
      				TheList\[i\].GetObjectData(info,context);
      				}
      			}
      		}
      	}
      
      class Program
      	{
      	static void Main(string\[\] args)
      		{
      		ObjectList MyObjectList=new ObjectList();
      		FileStream WriteStream=new FileStream("test.xml",FileMode.Create); 
      		SoapFormatter bfor=new SoapFormatter();
      		bfor.Serialize(WriteStream,MyObjectList);
      		}
      	}
      }
      

      In the sample I create a class MyObject. Objects of type MyObject are stored in an ObjectList object, which essentially is a wrapper around a LIST generic List object. I have no idea what I'm doing wrong. I did not find any samples on how to handle array serialization. Please note that the application I develop is far more complex than this. Therefore I do not want to use automatic serialization. I use the SOAP formatter because it is easier to debug. Later I will switch to the binary formatter. Tanks in advance for your advice. Rudolf Heijink

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Rudolf Jan Heijink wrote:

      ObjectList MyObjectList=new ObjectList();

      try this: List<MyObject> list = new List<MyObject>(); // now add the items to the list // bfor.Serialize(WriteStream, list);

      R 2 Replies Last reply
      0
      • L led mike

        Rudolf Jan Heijink wrote:

        ObjectList MyObjectList=new ObjectList();

        try this: List<MyObject> list = new List<MyObject>(); // now add the items to the list // bfor.Serialize(WriteStream, list);

        R Offline
        R Offline
        Rudolf Jan
        wrote on last edited by
        #3

        Thank you very much for your suggestion. Unfortunately, your suggestion does not work with the SOAP formmatter. The SOAP formatter seems not to support generic types. With the Binary formatter it seems to work, at least no exceptions or error messages. Still, I do not like it. I still think it is quite normal to serialize a number of objects. Another thing is that you should not invoke the Soap formatter more than once, because it generates XML headers. (i tried to do this) I also think there is a difference betwee a class containing al List and maybe other objects (as I will need in my application) and a list. So, I'm not happy yet. Thanks anway. Rudolf Heijink

        1 Reply Last reply
        0
        • L led mike

          Rudolf Jan Heijink wrote:

          ObjectList MyObjectList=new ObjectList();

          try this: List<MyObject> list = new List<MyObject>(); // now add the items to the list // bfor.Serialize(WriteStream, list);

          R Offline
          R Offline
          Rudolf Jan
          wrote on last edited by
          #4

          I did some additional tests. It seems the problem has nothing to do with lists, but possibly there is a bug in .NET. If I try to serialize two objects of the same type, I get the error message that I cannot serialize the same object twice. It mey be an error in the translation of the .NET error messages. I cannot check that. I have a Dutch Windows version trepresenting all .NET errors in Dutch. Below the code to demonstrate the problem:

          using System;
          using System.IO;
          using System.Collections.Generic;
          using System.Text;
          using System.Runtime.Serialization;
          using System.Xml;
          using System.Runtime.Serialization.Formatters.Soap;

          namespace Serialization
          {
          // Create a class to include in the list
          [Serializable]
          public class MyObject:ISerializable
          {
          public Int32 idx=0;

          	public MyObject(Int32 i)
          		{
          		idx=i;
          		}
          
          	internal MyObject(SerializationInfo info,StreamingContext context)
          		{
          		idx=info.GetInt32("Idx");
          		}
          
          	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
          		{
          		info.AddValue("Idx",idx);
          		}
          	}
          
          \[Serializable\]
          class ObjectList: ISerializable
          	{
          	MyObject object1;
          	MyObject object2;
          
          	public ObjectList()
          		{
          		object1=new MyObject(1);
          		object2=new MyObject(2);
          		}
          
          	protected ObjectList(SerializationInfo info,StreamingContext context)
          		{
          		object1=new MyObject(info,context);
          		object2=new MyObject(info,context);
          		}
          
          	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
          		{
          		object1.GetObjectData(info,context);
          		// here (netx statement) i get an exception, cmplaining i cannot serialize 
          		//the same object twice, though the objects are clearly not the same.
          		object2.GetObjectData(info,context);
          		}
          	}
          
          class Program
          	{
          	static void Main(string\[\] args)
          		{
          		ObjectList MyObjectList=new ObjectList();
          		FileStream WriteStream=new FileStream("test.xml",FileMode.Create); 
          		SoapFormatter bfor=new SoapFormatter();
          		MyObject Object1=new MyObject(1);
          		MyObject object2=new MyObject(2);
          		bfor.Serialize(WriteStream,MyObjectList);
          		}
          	}
          }
          

          Rudolf Heijink

          L 1 Reply Last reply
          0
          • R Rudolf Jan

            I did some additional tests. It seems the problem has nothing to do with lists, but possibly there is a bug in .NET. If I try to serialize two objects of the same type, I get the error message that I cannot serialize the same object twice. It mey be an error in the translation of the .NET error messages. I cannot check that. I have a Dutch Windows version trepresenting all .NET errors in Dutch. Below the code to demonstrate the problem:

            using System;
            using System.IO;
            using System.Collections.Generic;
            using System.Text;
            using System.Runtime.Serialization;
            using System.Xml;
            using System.Runtime.Serialization.Formatters.Soap;

            namespace Serialization
            {
            // Create a class to include in the list
            [Serializable]
            public class MyObject:ISerializable
            {
            public Int32 idx=0;

            	public MyObject(Int32 i)
            		{
            		idx=i;
            		}
            
            	internal MyObject(SerializationInfo info,StreamingContext context)
            		{
            		idx=info.GetInt32("Idx");
            		}
            
            	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
            		{
            		info.AddValue("Idx",idx);
            		}
            	}
            
            \[Serializable\]
            class ObjectList: ISerializable
            	{
            	MyObject object1;
            	MyObject object2;
            
            	public ObjectList()
            		{
            		object1=new MyObject(1);
            		object2=new MyObject(2);
            		}
            
            	protected ObjectList(SerializationInfo info,StreamingContext context)
            		{
            		object1=new MyObject(info,context);
            		object2=new MyObject(info,context);
            		}
            
            	public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
            		{
            		object1.GetObjectData(info,context);
            		// here (netx statement) i get an exception, cmplaining i cannot serialize 
            		//the same object twice, though the objects are clearly not the same.
            		object2.GetObjectData(info,context);
            		}
            	}
            
            class Program
            	{
            	static void Main(string\[\] args)
            		{
            		ObjectList MyObjectList=new ObjectList();
            		FileStream WriteStream=new FileStream("test.xml",FileMode.Create); 
            		SoapFormatter bfor=new SoapFormatter();
            		MyObject Object1=new MyObject(1);
            		MyObject object2=new MyObject(2);
            		bfor.Serialize(WriteStream,MyObjectList);
            		}
            	}
            }
            

            Rudolf Heijink

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            http://weblogs.asp.net/fbouma/archive/2005/12/12/432971.aspx[^]

            R 1 Reply Last reply
            0
            • L led mike

              http://weblogs.asp.net/fbouma/archive/2005/12/12/432971.aspx[^]

              R Offline
              R Offline
              Rudolf Jan
              wrote on last edited by
              #6

              Sad story, I think I understand much more now why things went wrong in my application. I will write an article about it for codeproject, so coming soon. Thanks anyway for your help. Rudolf Heijinbk

              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