Serialization problem
-
Hi ppl! May be you can help me with this: I have some collection inside collection of my classes and I need to serialize it. The tree looks like this: class NewsSourceCollection : CollectionBase, ISerializable { //This class is a main class and generally will hold all the data and List's void Add (NewsSourceSingle in_param) { this.List.Add(new NewsSourceSingle(in_param)); } void RemoveAt (int index) { this.List.RemoveAt(index); } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { //Question number 1: How I can serialize a List from System.Collection ??? } private newsSourcesColllection(SerializationInfo info, StreamingContext context) { //Here goes deserialization } } //class close class NewsSourceSingle : ISerializable { //This class will be used by NewsSourceCollection to Add or Remove from List and hold instance of NewsItemsCollection class to add some sub items there private NewsItemsCollection m_ns; public NewsSourceSingle () { //c'tor this.m_ns = new NewsItemsCollection (); } void Add(some_param) { this.m_s.Add(some_param); } . . . . +same functions to support ISerializable } class NewsItemsCollection : CollectionBase, ISerializable { //This class will hold all collection of NewsItems +the same function for supporing ISerializable }//class close class NewsItems : ISerializable { string pp; string dd; +same functions to support ISerializable } class close Question number 2: I can Formatter.Serialize (stream,object) but I can not (!) deserialize it !! Why ? Because Assembly of my type 'NewsProg version=1.0.9.245 Token....' is not found !!????? Question 3: How can I serialize List (of System.Collection) ?? "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)
-
Hi ppl! May be you can help me with this: I have some collection inside collection of my classes and I need to serialize it. The tree looks like this: class NewsSourceCollection : CollectionBase, ISerializable { //This class is a main class and generally will hold all the data and List's void Add (NewsSourceSingle in_param) { this.List.Add(new NewsSourceSingle(in_param)); } void RemoveAt (int index) { this.List.RemoveAt(index); } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { //Question number 1: How I can serialize a List from System.Collection ??? } private newsSourcesColllection(SerializationInfo info, StreamingContext context) { //Here goes deserialization } } //class close class NewsSourceSingle : ISerializable { //This class will be used by NewsSourceCollection to Add or Remove from List and hold instance of NewsItemsCollection class to add some sub items there private NewsItemsCollection m_ns; public NewsSourceSingle () { //c'tor this.m_ns = new NewsItemsCollection (); } void Add(some_param) { this.m_s.Add(some_param); } . . . . +same functions to support ISerializable } class NewsItemsCollection : CollectionBase, ISerializable { //This class will hold all collection of NewsItems +the same function for supporing ISerializable }//class close class NewsItems : ISerializable { string pp; string dd; +same functions to support ISerializable } class close Question number 2: I can Formatter.Serialize (stream,object) but I can not (!) deserialize it !! Why ? Because Assembly of my type 'NewsProg version=1.0.9.245 Token....' is not found !!????? Question 3: How can I serialize List (of System.Collection) ?? "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)
First, read Serializing Objects[^] in the .NET Framework SDK. If you attribute a class with
SerializableAttribute
, by default any private and public fields are serialized, so long as those types are serializable. Since you're deriving from theCollectionBase
- which uses theArrayList
internally, which is serializable - your class is already serializable. You don't need to implementISerializable
unless you want to override serialization. The second error is because the Type which is being deserialized is defined in an assembly that cannot be found. Make sure that when deserializing your assembly, your assembly can be found. See How the Runtime Locates Assemblies[^] in the .NET Framework SDK for more information. For the answer to your third question, see the answers above. Mosts lists in the .NET Framework are already serializable. For example, the following creates an XML document with the content of anArrayList
:using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
public class Test
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
SoapFormatter formatter = new SoapFormatter();
using (Stream s = new FileStream("Test.xml", FileMode.Create))
formatter.Serialize(s, list);
}
}Microsoft MVP, Visual C# My Articles