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

2016-02-19 20:59 31 1 收藏

下面图老师小编跟大家分享通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

【 tulaoshi.com - Web开发 】

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

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

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

  
  于是将他插入到ListEntity对象中

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

      ListEntity list = new ListEntity();
      Entity obj = new Entity();
      obj.Id = 1;
      obj.Name = "test";
      obj.Price = 3.23;
      list.Add(obj);
   这样,一个ListEntity对象就创建成功了,下面我们来将他序列化

          public static string SerializeBusinessObject(ListBusinessObject 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 = SerializeEntity(list);
   生成的XML文件为:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

      Root
          Item
              Id Type="Int32"1/Id
              Name Type="String"test/Name
              Price Type="Double"3.23/Price
          /Item
      /Root
  下面,我们根据上面生成的xml文件,将他反序列化,生成刚才的ListEntity对象

          public static ListBusinessObject DeserializeBusinessObject(string XmlStr)
          {
              ListBusinessObject result = new ListBusinessObject();
              XmlDocument XmlDoc = new XmlDocument();
              XmlDoc.LoadXml(XmlStr);
              foreach (XmlNode ItemNode in XmlDoc.GetElementsByTagName("Root").Item(0).ChildNodes)
              {
                  BusinessObject item = Activator.CreateInstanceBusinessObject();
                  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;
          }
   然后我们调用这个方法:

  ListEntity list = DeserializeEntity(str);
   完了。

  本文只是给大家介绍了序列化List对象的简单方法,用的时候要根据自己的情况而定。
  http://www.cnblogs.com/kchen/archive/2006/11/04/550382.html

来源:https://www.tulaoshi.com/n/20160219/1625008.html

延伸阅读
标签: PHP
本文转自:http://www.coolcode.cn/?p=170 1.前言 PHP (从 PHP 3.05 开始)为保存对象提供了一组序列化和反序列化的函数:serialize、unserialize。不过在 PHP 手册中对这两个函数的说明仅限于如何使用,而对序列化结果的格式却没做任何说明。因此,这对在其他语言中实现 PHP 方式的序列化来说,就比较麻烦了。虽然以前也搜集...
假设有一下一个实体类。 using System; using System.Xml; using System.Xml.Serialization; namespace TestPerson { public class Person { public string FullName; [NonSerialized()] public string Password; public Male sex; } public enum Male { M, F } } 先决定用xml 序列化把对象的状态dump到一个xml文件。 代...
我们知道,在java中,将一个非原型类型类型的对象引用,赋值给另一个对象的引用之后,这两个引用就指向了同一个对象,如: 代码如下: public class DeepCloneTest {  private class CloneTest {   private Long myLong = new Long(1);  }  public static void main(String args[]) {   new DeepCloneTes...
在很多应用中我们需要对数据进行保存,或是从介质上读取数据,这就涉及到文件的操作。我们可以利用各种文件存取方法完成这些工作,但MFC中也提供了一种读写文件的简单方法——“序列化”。序列化机制通过更高层次的接口功能向开发者提供了更利于使用和透明于字节流的文件操纵方法,举一个例来讲你可以将一个字串写入文件而不需要理会具体长度,...
内容提要:本文以简单的例子介绍在Visual C++编程中数据读写的基本方法和可序列化类的实现,并简单介绍了Visual C++中序列化的使用。 数据读写是应用程序中必不可少的一部分,Visual C++中数据的读写当然也十分重要,因此VisualC++在MFC中对数据的读写创造了十分好的支持,这使得我们可以十分方便的实现我们对数据读写操作的需要。 ...

经验教程

743

收藏

73
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部