当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法

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

点击次数:14 次 发布日期:2008-11-26 12:17:22 作者:源代码网
源代码网推荐      通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法
源代码网推荐  
源代码网推荐  我们在平时使用数据库的时候,经常会碰到一个问题,就是不希望数据实体对象插入数据库中, 却有想持久化的时候,那么就可以用序列化成
源代码网推荐  
源代码网推荐  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.cnblogs.com/kchen/archive/2006/11/04/550382.html
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华