我有一个存储自定义对象的 ArrayList。我想将该 ArrayList 序列化为字符串,以便可以将其保存在应用程序设置中。
这个问题看起来可以解决它,但在java中。而且我对 XML 并不聪明,所以有人可以帮忙吗? 序列化 Date 对象类型的 ArrayList
我有我的 ArrayList 设置:
...
MyObject tempObj = new MyObject("something",1,"something");
MyCollection.Add(tempObj);
...
我最初有这个。它输出字符串,但对象不存在:
private string SerializeArrayList(ArrayList obj)
{
System.Xml.XmlDocument doc = new XmlDocument();
Type[] extraTypes = new Type[1];
extraTypes[0] = typeof(MyObject);
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), extraTypes);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
return doc.InnerXml;
}
catch { throw; }
finally
{
stream.Close();
stream.Dispose();
}
}
编辑:代码请求
public class MyObject
{
private string eN;
private Boolean bE;
private int min;
private Boolean bot;
private string onE;
public MyObject(string na, Boolean b)
{
...
}
public MyObject()
{
}
public string GetSomething()
{
...