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

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

  • 相关阅读:
    Solaris+Oracle安装(详细图解)
    linux卸载和安装jdk
    UTF8, Unicode, GB2312格式串转换之C语言版
    linux安装ant
    在 Linux 平台下使用 JNI
    华为C/C++笔试题(1)
    c面试
    mongodb数据库
    YARN
    NPM(包管理器)
  • 原文地址:https://www.cnblogs.com/voidxy/p/1748349.html
Copyright © 2011-2022 走看看