Serialization of arrays
-
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
-
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
-
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);
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
-
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);
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
-
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
-
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