zoukankan      html  css  js  c++  java
  • 序列化与反序列化

    多的不说

      直接上代码好了。

    首先来发下实体类的代码,如下:

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyTestLab
    {
        [Serializable]
        
    public class XyiEntityList
        {
            
    private string _name;
            
    public string Name
            {
                
    set
                {
                    _name 
    = value;
                }
                
    get
                {
                    
    return _name;
                }
            }

            
    private int _age;
            
    public int Age
            {
                
    set
                {
                    _age 
    = value;
                }
                
    get
                {
                    
    return _age;
                }
            }

            
    private List<XyiEntity> _xyiList; 

            
    public  List<XyiEntity> XyiList
            {
                
    set
                {
                    _xyiList 
    = value;
                }
                
    get
                {
                    
    return _xyiList;
                }
            }
        }
    }


     然后说下,序列化:

    代码
     public static void WirteXML()
            {
                XyiEntityList InstanceList 
    = new XyiEntityList();
                InstanceList.Name 
    = x;
                InstanceList.Age 
    = 24;
                
    //InstanceList.XyiList = new List<XyiEntity>();

                List
    <XyiEntity> list = new List<XyiEntity>();

                XyiEntity x1 
    = new XyiEntity();
                x1.Age 
    = 1;
                x1.Name 
    = "x1";
                list.Add(x1);

                XyiEntity x2 
    = new XyiEntity();
                x2.Age 
    = 2;
                x2.Name 
    = "x2";
                list.Add(x2);

                XyiEntity x3 
    = new XyiEntity();
                x3.Age 
    = 3;
                x3.Name 
    = "x3";
                list.Add(x3);

                InstanceList.XyiList 
    = list; 

        
                    XmlSerializer myXmlSerializer 
    = new XmlSerializer(InstanceList.GetType ());
                    StreamWriter myWriter 
    = new StreamWriter("XMLFile/xy" + Guid.NewGuid().ToString() + ".xml");
                    myXmlSerializer.Serialize(myWriter, InstanceList); 
                
            }

    通过这样,我们可以把这个实体类序列化为一个XML文件。

    接下来我们看看反序列化:

    代码
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(
    "XMLFile/xy87e57597-a219-4020-90f9-9da6fe90bb08.xml");
                XmlDocument caseProperty 
    = xmlDoc; 
                XmlReader reader 
    = XmlReader.Create(new StringReader(caseProperty.OuterXml));
                XmlSerializer xs 
    = new XmlSerializer(typeof(XyiEntityList));
                XyiEntityList valueCase 
    = (XyiEntityList)xs.Deserialize(reader);

    就这么简单,是不是很简单啊··

  • 相关阅读:
    java----设计模式--创建型模式(GOF23)
    java----作用域
    java和python对比----实例化的对象属性:
    java----关键字
    java----Java的栈,堆,代码,静态存储区的存储顺序和位置
    java----面对对象
    算法----取0~30不重复的10个整数
    算法----二分查找算法
    织梦 验证码不显示问题
    dedecms 后台修改系统设置,但是config.cache.inc.php文件不能写入
  • 原文地址:https://www.cnblogs.com/voidxy/p/1748349.html
Copyright © 2011-2022 走看看