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


Posted in PHP onJune 08, 2013

概念: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自动生成月历代码
Oct 09 PHP
PHP 面向对象 PHP5 中的常量
May 05 PHP
PHP中date()日期函数有关参数整理
Jul 19 PHP
PHP中文件读、写、删的操作(PHP中对文件和目录操作)
Mar 06 PHP
thinkphp判断访客为手机端或PC端的方法
Nov 24 PHP
浅析iis7.5安装配置php环境
May 10 PHP
php准确获取文件MIME类型的方法
Jun 17 PHP
php四种定界符详解
Feb 16 PHP
PHP实现活动人选抽奖功能
Apr 19 PHP
Laravel学习教程之View模块详解
Sep 18 PHP
thinkphp5 模型实例化获得数据对象的教程
Oct 18 PHP
phpstorm激活码2020附使用详细教程
Sep 25 PHP
解析PHP自带的进位制之间的转换函数
Jun 08 #PHP
深入PHP内存相关的功能特性详解
Jun 08 #PHP
PHP rawurlencode与urlencode函数的深入分析
Jun 08 #PHP
PHP跳转页面的几种实现方法详解
Jun 08 #PHP
利用php递归实现无限分类 格式化数组的详解
Jun 08 #PHP
如何利用php array_multisort函数 对数据库结果进行复杂排序
Jun 08 #PHP
php引用返回与取消引用的详解
Jun 08 #PHP
You might like
discuz安全提问算法
2007/06/06 PHP
ThinkPHP分组下自定义标签库实例
2014/11/01 PHP
php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)
2017/02/04 PHP
PHP结合Vue实现滚动底部加载效果
2017/12/17 PHP
javascript-简单的日历实现及Date对象语法介绍(附图)
2013/05/30 Javascript
jQuery中$.each使用详解
2015/01/29 Javascript
基于jquery实现select选择框内容左右移动添加删除代码分享
2015/08/25 Javascript
详解JavaScript中的自定义事件编写
2016/05/10 Javascript
Node.js的特点详解
2017/02/03 Javascript
webpack学习--webpack经典7分钟入门教程
2017/06/28 Javascript
seaJs使用心得之exports与module.exports的区别实例分析
2017/10/13 Javascript
快速解决处理后台返回json数据格式的问题
2018/08/07 Javascript
JS apply用法总结和使用场景实例分析
2020/03/14 Javascript
小程序中使用css var变量(使js可以动态设置css样式属性)
2020/03/31 Javascript
[45:44]完美世界DOTA2联赛PWL S2 FTD vs PXG 第一场 11.27
2020/12/01 DOTA
使用Python抓取模板之家的CSS模板
2015/03/16 Python
以视频爬取实例讲解Python爬虫神器Beautiful Soup用法
2016/01/20 Python
python 简单备份文件脚本v1.0的实例
2017/11/06 Python
python3实现SMTP发送邮件详细教程
2018/06/19 Python
Python OpenCV之图片缩放的实现(cv2.resize)
2019/06/28 Python
flask实现验证码并验证功能
2019/12/05 Python
Python面向对象之继承原理与用法案例分析
2019/12/31 Python
详解Css3新特性应用之过渡与动画
2017/01/10 HTML / CSS
Html5适配iphoneX刘海屏的简单实现
2019/04/09 HTML / CSS
将世界上最美丽的摄影作品转化为艺术作品:Photos.com
2017/11/28 全球购物
澳大利亚优惠网站:Deals.com.au
2019/07/02 全球购物
好矿嫂事迹材料
2014/01/21 职场文书
迎元旦广播稿
2014/02/22 职场文书
老师的检讨书
2014/02/23 职场文书
工会主席事迹材料
2014/06/03 职场文书
公司采购主管岗位职责
2014/06/17 职场文书
抢劫罪辩护词
2015/05/21 职场文书
教师实习自我鉴定总结
2019/08/20 职场文书
Python 中random 库的详细使用
2021/06/03 Python
「约定的梦幻岛」作画发布诺曼生日新绘
2022/03/21 日漫
app场景下uniapp的扫码记录
2022/07/23 Java/Android