序列化对象的 ArrayList [英] Serialize ArrayList of Objects

查看:22
本文介绍了序列化对象的 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储自定义对象的 ArrayList.我想将该 ArrayList 序列化为字符串,以便将其保存在应用程序设置中.

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.

这个问题看起来可以解决它,但在java中.而且我对 XML 并不聪明,所以有人可以帮忙吗?序列化Date对象类型的ArrayList

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

我有我的 ArrayList 设置:

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

代码请求

    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()
        {
            ...

推荐答案

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

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.

我的测试:

    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()
    {
    }
}

这篇关于序列化对象的 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆