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

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

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

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<>对象的简单方法,用的时候要根据自己的情况而定。


posted on 2006-11-04 23:20 kchen 阅读(949) 评论(5)   编辑 收藏 所属分类: .Net Framework

Feedback

#1楼  2007-03-17 17:52 清 [未注册用户]
好文,写得真详细
   回复   引用   查看     

这是泛型List<SerialEmployee> emp = new List<SerialEmployee>();

这是泛型中的对象public class SerialEmployee
{
public int EmployeeID;
public string LastName;
public string FirstName;
public int YearsService;
public double Salary;
public SerialEmployee(int EmployeeID, string LastName, string FirstName, int YearsService, double Salary)
{
this.EmployeeID = EmployeeID;
this.LastName = LastName;
this.FirstName = FirstName;
this.YearsService = YearsService;
this.Salary = Salary;


}
我将这泛型中添加数据后,发生到远程客户端
我该如何反序列化它,然后来读取泛型中的成员!
问题补充:emp链表中的对象SerialEmployee在
emp.Add(new SerialEmployee(1, "cehnli", "Li", 33,43.44));
这后,序列化,通过socket发送到对方主机,对方主机应该如何反序列化它
他是c#的应用程序
   回复   引用   查看     

你写的我读得,很难懂啊
   回复   引用   查看     

public static List<BusinessObject> Deserialize<BusinessObject>(string XmlStr)
这个List<BusinessObject> Deserialize<BusinessObject>(是这个方法的名称吗
   回复   引用   查看    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值