序列化对象的ArrayList [英] Serialize ArrayList of Objects
问题描述
我有一个用来保存自定义对象的ArrayList。我想这ArrayList的序列化到一个字符串,这样我就可以将其保存在应用程序里面设置。
这个问题看来解决它,但在java中。而且我不聪明与XML,所以会有人帮帮忙?
<一href=\"http://stackoverflow.com/questions/11615206/serialize-an-arraylist-of-date-object-type\">Serialize Date对象类型的ArrayList
我有我的ArrayList设置:
...
为MyObject TEMPOBJ =新MyObject来(东西,1,东西);
MyCollection.Add(TEMPOBJ);
...
和我本来这个。它输出串,但对象是不存在
私人字符串SerializeArrayList(ArrayList的OBJ)
{
System.Xml.XmlDocument DOC =新的XmlDocument();
键入[] = extraTypes新型[1];
extraTypes [0] = typeof运算(myObject的);
System.Xml.Serialization.XmlSerializer串行=新System.Xml.Serialization.XmlSerializer(typeof运算(ArrayList中),extraTypes);
System.IO.MemoryStream流=新System.IO.MemoryStream();
尝试
{
serializer.Serialize(流OBJ);
stream.Position = 0;
doc.Load(流);
返回doc.InnerXml;
}
赶上{抛出; }
最后
{
stream.Close();
stream.Dispose();
}
}
编辑:code请求
公共类为MyObject
{
私人字符串连接;
私人布尔定;
私人诠释分钟;
私人布尔机器人;
私人字符串之一;
公众为MyObject(字符串呐,布尔B)
{
...
}
公众为MyObject()
{
} 公共字符串GetSomething()
{
...
我测试了code和它似乎工作正常,只要你有 [Serializable接口]
在对象上。
此外,如果你正在尝试序列化等领域,你必须让他们的公共属性。
我的测试:
ArrayList的阵列=新的ArrayList();
规则TEMPOBJ =新规则{一=值,分= 45,EN =值};
array.Add(TEMPOBJ);
字符串结果= SerializeArrayList(数组); 私人字符串SerializeArrayList(ArrayList的OBJ)
{
XmlDocument的DOC =新的XmlDocument();
XmlSerializer的序列化=新的XmlSerializer(typeof运算(ArrayList中),新类型[] {typeof运算(规则)});
使用(MemoryStream的流=新System.IO.MemoryStream())
{
尝试
{
serializer.Serialize(流OBJ);
stream.Position = 0;
doc.Load(流);
返回doc.InnerXml;
}
赶上(异常前)
{
}
}
返回的String.Empty;
}
对象:
[Serializable接口]
[XmlType将(类型名=规则)]
公共类规则
{
//制作领域propertys所以他们将被序列化
公共字符串连接{搞定;组; } //名称
公共布尔为{搞定;组; } //是否完全封锁
公众诠释分钟{搞定;组; } //分钟后,他们被允许,如果受阻
公共布尔BOT {搞定;组; } //创建通知,如果超过免税额
公共字符串之一{搞定;组; } // Nothing或结算流程 公共规则(字符串呐,布尔B)
{ } 公共规则()
{
}
}
I have an ArrayList which stores a custom object. I want to serialize that ArrayList to a string so I can save it inside the Application settings.
This question looks to resolve it, but is in java. And I am not smart with XML, so could someone help out? Serialize an ArrayList of Date object type
I have my ArrayList setup:
...
MyObject tempObj = new MyObject("something",1,"something");
MyCollection.Add(tempObj);
...
And I originally had this. It outputs the string, but the object isn't there:
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();
}
}
EDIT: Code request
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()
{
...
I tested your code and it seems to work ok, as long as you have [Serializable]
on your object.
Also if you are trying to Serialize the fields, you will have to make them public properties.
My Test:
ArrayList array = new ArrayList();
Rules tempObj = new Rules { onE = "Value", min = 45, eN = "Value" };
array.Add(tempObj);
string result = SerializeArrayList(array);
private string SerializeArrayList(ArrayList obj)
{
XmlDocument doc = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), new Type[]{typeof(Rules)});
using (MemoryStream stream = new System.IO.MemoryStream())
{
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
return doc.InnerXml;
}
catch (Exception ex)
{
}
}
return string.Empty;
}
Object:
[Serializable]
[XmlType(TypeName = "Rules")]
public class Rules
{
// Make fields propertys so they will be serialized
public string eN { get; set; } //Name
public Boolean bE { get; set; } //Whether blocked entirely
public int min { get; set; } //Minutes they are allowed if blocked
public Boolean bot { get; set; } //Create notification if allowance exceed
public string onE { get; set; } //Nothing or CLOSE Process
public Rules(string na, Boolean b)
{
}
public Rules()
{
}
}
这篇关于序列化对象的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!