Serialization of ArrayList of ArrayLists




In my application, I have the MainForm.aspx.cs (where all the
functions are defined) and ClasssificationInfo.cs class. This is the
Classification Info class --

using System;
using System.Xml.Serialization ;

namespace MyAppl
public class ClassificationInfo
public string Name1 ;
public string Name2 ;
public string Name3 ;

public ClassificationInfo()
Name1 = "";
Name2 = "" ;
Name3 = "" ;

In my MainForm class I declared this: public ClassificationInfo
oClassificationInfo = new ClassificationInfo() ;

Whenever a user clicks a button on the page, I will collect all the
information from the textboxes (Name1, Name2, Name3) and store in a
Temporary arraylist and then I store it in the session. On the dispose
event, I'm serializing the data like this...

public void SerializeData()
string FiletoSave = @"C:\Temp\Classification.xml";
StreamWriter sw = new StreamWriter(FiletoSave, true);
ArrayList ToSerializeList = new ArrayList() ;
ToSerializeList = (ArrayList) this.Page.Session["CollList"] ;
XmlSerializer TheXMLSerializer =new XmlSerializer(typeof(ArrayList)) ;
TheXMLSerializer.Serialize(sw, ToSerializeList);
sw.Close() ;

When I run this code, I'm getting error saying:
The type MyAppl.ClassificationInfo was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.

How should I solve this problem?

Any help would be greatly appreciated.





The XmlInclude on ClassificationInfo won't do the trick -- if you were going
to use the XmlInclude attribute, it would have to be on ArrayList (saying to
the CLR, "When you try to serialize this ArrayList, you're going to run into
objects of type ClassificationInfo"), but you can't modify its definition.
Try using an XmlSerializer constructor with the extraTypes parameter, like

Type [ ] extraTypes = new Type[ 1 ];
extraTypes[ 0 ] = typeof( ClassificationInfo )
XmlSerializer TheXMLSerializer = new XmlSerializer( typeof( ArrayList ),
extraTypes ) ;



Mark Rockmann

change the line
XmlSerializer TheXMLSerializer =new XmlSerializer(typeof(ArrayList)) ;
XmlSerializer TheXMLSerializer
= new XmlSerializer(typeof(ArrayList),
new Type[] { typeof(ClassificationInfo) });

BTW, you should really use a strongly typed collection. Look for
CollectionBase in the docs.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

.NET XmlSerializer can serialize xs:documentation elements? 1
file [?] 7
Shouldn't this compile? 2
Serialization of Inherited Class 4
Can't serialize ArrayList 2
ArrayList 12
ArrayList help 2
arraylist to array 8