0

我有一个存储自定义对象的 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()
        {
            ...
4

2 回答 2

5

我测试了你的代码,它似乎工作正常,只要你有[Serializable]你的对象。

此外,如果您尝试对字段进行序列化,则必须将它们设为公共属性。

我的测试:

    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;
    }

目的:

[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()
    {
    }
}
于 2013-02-03T22:43:10.250 回答
3

我遇到了类似的问题,有一个很棒的程序,叫做 SharpSerializer,可以通过 Nuget 获得。它将很容易地处理您的 ArrayList,只需键入代码:

 SharpSerializer mySerializer = new SharpSerializer();
 mySerializer.Serialize(ArrayList, "filetosaveto.xml");

这是网站的链接,它是免费的,所以不用担心支付任何费用:

http://www.sharpserializer.com/en/index.html

于 2013-02-03T20:54:58.987 回答