国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

通過序列化和反序列化泛型數據實體集合來實現持久化數據對象的方法

2019-11-18 16:53:46
字體:
來源:轉載
供稿:網友

通過序列化和反序列化泛型數據實體集合來實現持久化數據對象的方法

我們在平時使用數據庫的時候,經常會碰到一個問題,就是不希望數據實體對象插入數據庫中, 卻有想持久化的時候,那么就可以用序列化成

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<>對象的簡單方法,用的時候要根據自己的情況而定。
http://www.survivalescaperooms.com/kchen/archive/2006/11/04/550382.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荆州市| 都安| 石门县| 兴文县| 基隆市| 西安市| 铁力市| 广西| 莆田市| 浦江县| 文山县| 黑河市| 闵行区| 吴川市| 屯昌县| 鲁甸县| 积石山| 芷江| 湛江市| 九江县| 宁城县| 乌拉特中旗| 锦屏县| 镇巴县| 安阳市| 隆林| 吴江市| 博罗县| 通河县| 会昌县| 卫辉市| 平潭县| 潢川县| 新竹县| 措美县| 昭通市| 三河市| 米林县| 历史| 原阳县| 平乐县|