ArrayList aList = new ArrayList();
aList.Add("aaa");
aList.Add("bbb");
XmlSerializer ser = new XmlSerializer(typeof(ArrayList));
System.IO.MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Default);
ser.Serialize(writer, aList);
writer.Close();
string strtmp=Encoding.Default.GetString(mem.ToArray());
MessageBox.Show(strtmp);
//看!这里串行为什么了,串行为一个xml了!!
XmlSerializer mySerializer = new XmlSerializer(typeof(ArrayList));
StreamReader mem2 = new StreamReader(new MemoryStream(System.Text.Encoding.Default.GetBytes(strtmp)), System.Text.Encoding.Default);
ArrayList myObject = (ArrayList)mySerializer.Deserialize(mem2);
MessageBox.Show(myObject[0].ToString());
//这里反序列化为对象,测试成功了!!
//
private string myXmlSerializer(object o)
{
XmlSerializer ser = new XmlSerializer(o.GetType());
System.IO.MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Default);
ser.Serialize(writer, o);
writer.Close();
return Encoding.Default.GetString(mem.ToArray());
}
private object myXmlDeserialize(string s, Type t)
{
XmlSerializer mySerializer = new XmlSerializer(t);
StreamReader mem2 = new StreamReader(new MemoryStream(System.Text.Encoding.Default.GetBytes(s)), System.Text.Encoding.Default);
return mySerializer.Deserialize(mem2);
}