首页 > 开发 > 综合 > 正文

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法

2024-07-21 02:28:30
字体:
来源:转载
供稿:网友

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法

我们在平时使用数据库的时候,经常会碰到一个问题,就是不希望数据实体对象插入数据库中, 却有想持久化的时候,那么就可以用序列化成

xml字符串,来保存到其他地方,由于生成的是字符串,所以可以保存到任意我们想保存的地方。比如 asp.net的viewstate,cookie,cache等。

首先,我们定义一个数据实体类。

 

    class entity
    {
        public entity()
        {}
        private int id;
        public int id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        private string name;
        public string name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private double price;
        public double price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
    }


于是将他插入到list<entity>对象中

    list<entity> list = new list<entity>();
    entity obj = new entity();
    obj.id = 1;
    obj.name = "test";
    obj.price = 3.23;
    list.add(obj);
 这样,一个list<entity>对象就创建成功了,下面我们来将他序列化

        public static string serialize<businessobject>(list<businessobject> genericlist)
        {
            xmldocument result = new xmldocument();
            result.loadxml("<root></root>");
            foreach (businessobject obj in genericlist)
            {
                xmlelement item = result.createelement("item");
                propertyinfo[] properties = obj.gettype().getproperties();
                foreach (propertyinfo property in properties)
                {
                    if (property.getvalue(obj, null) != null)
                    {
                        xmlelement element = result.createelement(property.name);
                        element.setattribute("type", property.propertytype.name);
                        element.innertext = property.getvalue(obj, null).tostring();
                        item.appendchild(element);
                    }
                }
                result.documentelement.appendchild(item);
            }
            return result.innerxml;
        }
 然后我们调用这个方法

string str = serialize<entity>(list);
 生成的xml文件为:

    <root>
        <item>
            <id type="int32">1</id>
            <name type="string">test</name>
            <price type="double">3.23</price>
        </item>
    </root>
下面,我们根据上面生成的xml文件,将他反序列化,生成刚才的list<entity>对象

        public static list<businessobject> deserialize<businessobject>(string xmlstr)
        {
            list<businessobject> result = new list<businessobject>();
            xmldocument xmldoc = new xmldocument();
            xmldoc.loadxml(xmlstr);
            foreach (xmlnode itemnode in xmldoc.getelementsbytagname("root").item(0).childnodes)
            {
                businessobject item = activator.createinstance<businessobject>();
                propertyinfo[] properties = typeof(businessobject).getproperties();
                foreach (xmlnode propertynode in itemnode.childnodes)
                {
                    string name = propertynode.name;
                    string type = propertynode.attributes["type"].value;
                    string value = propertynode.innerxml;
                    foreach (propertyinfo property in properties)
                    {
                        if (name == property.name)
                        {
                            property.setvalue(item,convert.changetype(value,property.propertytype), null);
                        }
                    }
                }
                result.add(item);
            }
            return result;
        }
 然后我们调用这个方法:

list<entity> list = deserialize<entity>(str);
 完了。

本文只是给大家介绍了序列化list<>对象的简单方法,用的时候要根据自己的情况而定。

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表