Serialization Question
-
I created an object class derived from System.Object and an array class derived from ArrayList. I'm trying to serialize (and deserialize) the entire thing. I found a great guide here. When I serialized in the same situation in VC++, it automatically serialized all of the objects in the array for me. All I did was write in private variables that are in the list itself, and all the objects inside the list got serialized properly too. Now, in C#, I read the file after serialization, it picks up the private array list data but it doesn't automatically serialize the objects inside of it. Not only do they not show up in the resulting file, but there's a breakpoint on the individual object serialization which never gets reached. The question is, is the array list not supposed to automatically serialize it's objects inside of it, or am I not doing something to let it automatically serialize its members? I can serialize the members using a foreach but I wanted to know what the word on this was so I don't re-invent the wheel.
-
I created an object class derived from System.Object and an array class derived from ArrayList. I'm trying to serialize (and deserialize) the entire thing. I found a great guide here. When I serialized in the same situation in VC++, it automatically serialized all of the objects in the array for me. All I did was write in private variables that are in the list itself, and all the objects inside the list got serialized properly too. Now, in C#, I read the file after serialization, it picks up the private array list data but it doesn't automatically serialize the objects inside of it. Not only do they not show up in the resulting file, but there's a breakpoint on the individual object serialization which never gets reached. The question is, is the array list not supposed to automatically serialize it's objects inside of it, or am I not doing something to let it automatically serialize its members? I can serialize the members using a foreach but I wanted to know what the word on this was so I don't re-invent the wheel.
Did you mark the class inherited from ArrayList with the [Serializable] attribute? As far as I know, serialization cannot be inherited, but I am not sure. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
I created an object class derived from System.Object and an array class derived from ArrayList. I'm trying to serialize (and deserialize) the entire thing. I found a great guide here. When I serialized in the same situation in VC++, it automatically serialized all of the objects in the array for me. All I did was write in private variables that are in the list itself, and all the objects inside the list got serialized properly too. Now, in C#, I read the file after serialization, it picks up the private array list data but it doesn't automatically serialize the objects inside of it. Not only do they not show up in the resulting file, but there's a breakpoint on the individual object serialization which never gets reached. The question is, is the array list not supposed to automatically serialize it's objects inside of it, or am I not doing something to let it automatically serialize its members? I can serialize the members using a foreach but I wanted to know what the word on this was so I don't re-invent the wheel.
You must put XMLArray and XmlArrayItem attributes as shown in code below:
[XmlArray ("MyItems"), XmlArrayItem("MyItem", typeof(MyItem))] public ArrayList MyItems = new ArrayList();
The output XML should be in following form<MyItems> <MyItem/> <MyItem/> </MyItems>
Of course the MyItem class must be serializable too.. :) DevIntelligence.com - My blog for .Net Developers -
Did you mark the class inherited from ArrayList with the [Serializable] attribute? As far as I know, serialization cannot be inherited, but I am not sure. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
My class that I derived from ArrayList, yeah. I have private variables associated with the list itself that are declared in the lists' class definition like counts, maximums and minimums. Those get serialized and deserialized so I know C# has no problem finding the list.
-
You must put XMLArray and XmlArrayItem attributes as shown in code below:
[XmlArray ("MyItems"), XmlArrayItem("MyItem", typeof(MyItem))] public ArrayList MyItems = new ArrayList();
The output XML should be in following form<MyItems> <MyItem/> <MyItem/> </MyItems>
Of course the MyItem class must be serializable too.. :) DevIntelligence.com - My blog for .Net DevelopersI'm trying to use the binary formatter instead of XML or Soap.
-
My class that I derived from ArrayList, yeah. I have private variables associated with the list itself that are declared in the lists' class definition like counts, maximums and minimums. Those get serialized and deserialized so I know C# has no problem finding the list.
Could you post the code of the 2 classes, please? -------- "I say no to drugs, but they don't listen." - Marilyn Manson