Serialization
-
Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.
-
Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.
-
Yeah i got it i should Derive VariableCollection from CollectionBase and strongly Type the collection to be of Type Variable... Thanks TusharM
-
Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.
The
ArrayList
isSerializable
, you should check out the XmlArrayAttribute[^] class, this can be used to tell yourXmlSerializer
to serialize a particular class member as an array of XML elements. - Nick Parker
My Blog | My Articles