探讨:使用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实现手机归属地查询API接口实现代码
Aug 27 PHP
整理的一些实用WordPress后台MySQL操作命令
Jan 07 PHP
php 深入理解strtotime函数的使用详解
May 23 PHP
php 删除目录下N分钟前创建的所有文件的实现代码
Aug 10 PHP
Windows中使用计划任务自动执行PHP程序实例
May 09 PHP
PHP+Memcache实现wordpress访问总数统计(非插件)
Jul 04 PHP
PHP中feof()函数实例测试
Aug 23 PHP
PHP多进程编程实例
Oct 15 PHP
php无限分类使用concat如何实现
Nov 05 PHP
PHP使用mkdir创建多级目录的方法
Dec 22 PHP
Yii2使用自带的UploadedFile实现的文件上传
Jun 20 PHP
php实时倒计时功能实现方法详解
Feb 27 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
非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程
2016/04/12 PHP
详解PHP用substr函数截取字符串中的某部分
2016/12/03 PHP
Yii框架中使用PHPExcel的方法分析
2019/07/25 PHP
学习YUI.Ext 第二天
2007/03/10 Javascript
js操作二级联动实现代码
2010/07/27 Javascript
JS保存和删除cookie操作 判断cookie是否存在
2013/11/13 Javascript
JavaScript的常见兼容问题及相关解决方法(chrome/IE/firefox)
2013/12/31 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
jQuery中after()方法用法实例
2014/12/25 Javascript
jQuery实现360°全景拖动展示
2015/03/18 Javascript
关于axios返回空对象的问题解决
2017/04/04 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
详解Vue路由钩子及应用场景(小结)
2017/11/07 Javascript
基于vue中解决v-for使用报红并出现警告的问题
2018/03/03 Javascript
[02:57]2014DOTA2国际邀请赛-观众采访
2014/07/19 DOTA
[00:44]TI7不朽珍藏III——军团指挥官不朽展示
2017/07/15 DOTA
[55:32]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第二场
2018/04/05 DOTA
收集的几个Python小技巧分享
2014/11/22 Python
python持久性管理pickle模块详细介绍
2015/02/18 Python
python3的url编码和解码,自定义gbk、utf-8的例子
2019/08/22 Python
win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程
2019/12/03 Python
在django中使用post方法时,需要增加csrftoken的例子
2020/03/13 Python
sklearn的predict_proba使用说明
2020/06/28 Python
使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的方法
2014/04/03 HTML / CSS
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
吃透移动端 Html5 响应式布局
2019/12/16 HTML / CSS
毕业生应聘幼儿园的自荐信
2013/11/20 职场文书
北体毕业生求职信
2014/02/28 职场文书
阳光体育活动总结
2014/04/30 职场文书
三关爱志愿服务活动方案
2014/08/17 职场文书
如何写贫困证明申请书
2014/10/29 职场文书
医德医风自我评价2015
2015/03/03 职场文书
2015年前台接待工作总结
2015/05/04 职场文书
浪漫婚礼主持词开场白
2015/11/24 职场文书
导游词之安徽巢湖
2019/12/26 职场文书
【海涛dota解说】一房久违的影魔魂守二连发
2022/04/01 DOTA