探讨:使用XMLSerialize 序列化与反序列化

 更新时间:2013年06月08日 17:30:12   作者:  
本篇文章是对使用XMLSerialize 序列化与反序列化进行了详细的分析介绍,需要的朋友参考下
概念:XML序列化是将公共字段和属性转化为序列格式(这里指XML),以便存储或传输的过程。反序列化则是从XML中重新创建原始状态的对象.
复制代码 代码如下:

    class SerializeDemo
    {
        static void Main()
        {
            EmployeeCollection employeeCollection = new EmployeeCollection()
            {
                Employees = Employeer.Employees()
            };
            XmlSerializer serialize = new XmlSerializer(typeof(EmployeeCollection));
            string filePath = @"E:\PProject\Test\Employee.xml";
             SerializeEmployee(serialize, filePath, employeeCollection);
            DeserializeEmployee(serialize, filePath);
        }
        static void SerializeEmployee(XmlSerializer serialize, string filePath, EmployeeCollection employeeCollection)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                serialize.Serialize(fs, employeeCollection);
            }
        }
        static void DeserializeEmployee(XmlSerializer serialize,string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                EmployeeCollection collection = (EmployeeCollection)serialize.Deserialize(fs);
                collection.Employees.ForEach(e => Console.WriteLine("Name:{0},Gender:{1},Age:{2},Education:{3}", e.userName, e.gender, e.age, e.education));
            }
        }
    }
    [Serializable]
    public class EmployeeCollection
    {
        public List<Employeer> Employees { get; set; }
    }
    [Serializable]
    public class Employeer
    {
        public string userId { get; set; }
        public string userName { get; set; }
        public string gender { get; set; }
        public int age { get; set; }
        public List<WorkExperience> workExperience { get; set; }
        public string education { get; set; }
        public static List<Employeer> Employees()
        {
           return new List<Employeer>()
           {
                new Employeer()
                {  
                    userId = "0001",
                    userName = "guoHu",
                    gender="Man",
                    age=25,education="underGraduate",
                    workExperience = WorkExperience.GetWorkExperience("0001")
                }
           };

        }
    }
    [Serializable]
    public class WorkExperience
    {
        public string userId { get; set; }
        public string companyName { get; set; }
        public string seniority { get; set; }

        public static List<WorkExperience> GetWorkExperience(string userId)
        {
            List<WorkExperience> workExperience = new List<WorkExperience>();
            Unity unity = Unity.GetInstance();
            DataTable table = new DataTable();
            unity.GetTable(out table);

            var experiences = (from experience in table.AsEnumerable()
                               where experience.Field<string>("UserId") == userId
                               select new
                               {
                                   companyName = experience.Field<string>("CompanyName"),
                                   seniority = experience.Field<string>("Seniority")
                               }).ToList();
            experiences.ForEach(e => workExperience.Add(new WorkExperience() { companyName = e.companyName, seniority = e.seniority }));
            return workExperience;
        }
    }
    public class Unity
    {
        public static DataTable tables = new DataTable();
        public static DataRow dr;
        public static DataColumn dc = new DataColumn();
        public static object objLock = new object();
        public static Unity unityInstance;
        private Unity()
        {

        }
        public static Unity GetInstance()
        {
            if (unityInstance == null)
            {
                lock (objLock)
                {
                    if (unityInstance == null)
                    {
                        unityInstance = new Unity();
                    }
                }
            }
            return unityInstance;
        }
        public void GetTable(out DataTable dt)
        {
            unityInstance.CreateTable();

            dr = tables.NewRow();
            dr["UserId"] = "0001";
            dr["CompanyName"] = "WireSoft";
            dr["Seniority"] = "2012.02-2012.05";
            tables.Rows.Add(dr);
            dr = tables.NewRow();
            dr["UserId"] = "0001";
            dr["CompanyName"] = "Jin Xun";
            dr["Seniority"] = "2009.07-2011.02";
            tables.Rows.Add(dr);
            dr = tables.NewRow();
            dr["UserId"] = "0002";
            dr["CompanyName"] = "Hua Wei";
            dr["Seniority"] = "2011.07-";
            tables.Rows.Add(dr);
            dt = tables.Copy();
        }
        public  void CreateTable()
        {
            dc = new DataColumn("UserId", System.Type.GetType("System.String"));
            tables.Columns.Add(dc);
            dc = new DataColumn("companyName", System.Type.GetType("System.String"));
            tables.Columns.Add(dc);
            dc = new DataColumn("seniority", System.Type.GetType("System.String"));
            tables.Columns.Add(dc);
        }
    }

相关文章

  • 详解PHP数组赋值方法

    详解PHP数组赋值方法

    这篇文章主要介绍了详解PHP数组赋值方法,文章就怎样创建数组、怎样给PHP数组赋值,文章都做了详细的介绍和讲解,希望对大家有帮助。
    2015-11-11
  • php中laravel调度执行错误解决方法

    php中laravel调度执行错误解决方法

    在本篇内容里小编给大家整理的是一篇关于php中laravel调度执行错误解决方法,对此有兴趣的朋友们可以学习参考下。
    2021-02-02
  • php+jQuery ajax实现的实时刷新显示数据功能示例

    php+jQuery ajax实现的实时刷新显示数据功能示例

    这篇文章主要介绍了php+jQuery ajax实现的实时刷新显示数据功能,结合实例形式分析了php结合jQuery ajax实时刷新读取显示数据库数据相关操作技巧,需要的朋友可以参考下
    2019-09-09
  • 11个PHPer必须要了解的编程规范

    11个PHPer必须要了解的编程规范

    从设计之初,PHP被广泛用于开发基于Web的应用程序。 由于PHP是一种脚本语言,开发的时候必须遵守一些规范。
    2014-09-09
  • 解析PHP可变函数的经典用法

    解析PHP可变函数的经典用法

    本篇文章是对PHP可变函数的经典用法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP遍历文件夹与文件类及处理类用法实例

    PHP遍历文件夹与文件类及处理类用法实例

    这篇文章主要介绍了PHP遍历文件夹与文件类及处理类用法实例,包括了文件及文件夹的遍历以及清除utf8的bom头方法,非常实用,需要的朋友可以参考下
    2014-09-09
  • 兼容firefox,chrome的网页灰度效果

    兼容firefox,chrome的网页灰度效果

    今天全天下网页都变灰了对吧,话说我对这种强制行为很不解。哀悼与否在于一个人的内心是否善良。表面上的让网页没有颜色,让视频网站不能搜索,究竟有多大意义呢?
    2011-08-08
  • PHP 模板高级篇总结

    PHP 模板高级篇总结

    PHP 模板高级篇总结...
    2006-12-12
  • php中define用法实例

    php中define用法实例

    这篇文章主要介绍了php中define用法,实例分析了php使用define定义常量的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • PHP中替换键名的简易方法示例详解

    PHP中替换键名的简易方法示例详解

    默认输出的时候,将数据库字段名作为数组的键名进行输出,但带有键名的数据不能够满足未知情况下的操作,下面为大家介绍个不错的方法可以解决这个问题
    2014-01-01

最新评论