//序列化
public string SerializeArrayList(ArrayList al)
{
Type[] extra = new Type[1];
extra[0] = typeof(DownloadInfo);
XmlSerializer xs = new XmlSerializer(typeof(ArrayList), extra);
MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.Default);
xs.Serialize(tw, al);
tw.Close();
return Encoding.Default.GetString(ms.ToArray());
}
//反序列化
public ArrayList DeserializeArrayList(string serQuestions, Type type, Type[] extratype)
{
XmlSerializer xs = new XmlSerializer(type, extratype);
StreamReader sr = new StreamReader(new MemoryStream(System.Text.Encoding.Default.GetBytes(serQuestions)), System.Text.Encoding.Default);
return (ArrayList)xs.Deserialize(sr);
}
调用方法:
SerializeArrayList(ArrayList);
Type[] extra = new Type[1];
extra[0] = typeof(自定义类);
ArrayList arr = DeserializeArrayList(strArr, typeof(ArrayList), extra);