Serialization of ArrayList of ArrayLists

S

Steven

Hello,

In my asp.net 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
{
[XmlInclude(typeof(ClassificationInfo))]
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.

Thanks
Steven
 
G

Guest

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
this:

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

Mark Rockmann

Hello,
change the line
XmlSerializer TheXMLSerializer =new XmlSerializer(typeof(ArrayList)) ;
to
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.

Mark
 

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

Top