探讨:使用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 相关文章推荐
拼音码表的生成
Oct 09 PHP
图形数字验证代码
Oct 09 PHP
php MsSql server时遇到的中文编码问题
Jun 11 PHP
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
Mar 15 PHP
php park、unpark、ord 函数使用方法(二进制流接口应用实例)
Oct 19 PHP
PHP性能优化准备篇图解PEAR安装
Dec 05 PHP
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
Mar 18 PHP
PHP获取数组的键与值方法小结
Jun 13 PHP
深入剖析PHP中printf()函数格式化使用
May 23 PHP
php使用QueryList轻松采集js动态渲染页面方法
Sep 11 PHP
laravel框架邮箱认证实现方法详解
Nov 22 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
将RTF格式的文件转成HTML并在网页中显示的代码
2006/10/09 PHP
PHP常用函数小技巧
2008/09/11 PHP
PHP将DateTime对象转化为友好时间显示的实现代码
2011/09/20 PHP
php 搜索框提示(自动完成)实例代码
2012/02/05 PHP
PHP内核探索:变量概述
2014/01/30 PHP
PHP也能干大事 随机函数
2015/04/14 PHP
PHP explode()函数用法讲解
2019/02/15 PHP
javascript面向对象编程代码
2011/12/19 Javascript
jqPlot 图表中文API使用文档及源码和在线示例
2012/02/07 Javascript
JavaScript模仿Pinterest实现图片预加载功能
2016/10/25 Javascript
详解百度百科目录导航树小插件
2017/01/08 Javascript
微信小程序url传参写变量的方法
2018/08/09 Javascript
JavaScript实现动态留言板
2020/03/16 Javascript
es6函数中的作用域实例分析
2020/04/18 Javascript
vue用elementui写form表单时,在label里添加空格操作
2020/08/13 Javascript
angular8.5集成TinyMce5的使用和详细配置(推荐)
2020/11/16 Javascript
[03:44]2014DOTA2国际邀请赛 71专访:DK战队赛前讨论视频遭泄露
2014/07/13 DOTA
python字符串编码识别模块chardet简单应用
2015/06/15 Python
使用matplotlib画散点图的方法
2018/05/25 Python
使用sklearn之LabelEncoder将Label标准化的方法
2018/07/11 Python
Python GUI布局尺寸适配方法
2018/10/11 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
2018/10/21 Python
Python中extend和append的区别讲解
2019/01/24 Python
pytorch中的自定义反向传播,求导实例
2020/01/06 Python
python使用Word2Vec进行情感分析解析
2020/07/31 Python
html5的canvas实现3d雪花飘舞效果
2013/12/27 HTML / CSS
如何设置Java的运行环境
2013/04/05 面试题
可以使用抽象函数重写基类中的虚函数吗
2013/06/02 面试题
预备党员思想汇报范文
2013/12/29 职场文书
职工运动会邀请函
2014/01/19 职场文书
中华美德颂演讲稿
2014/05/20 职场文书
员工辞职信范文大全
2015/05/12 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
2019最新校园运动会广播稿!
2019/06/28 职场文书
承诺书应该怎么写?
2019/09/10 职场文书
golang 实用库gotable的具体使用
2021/07/01 Golang