探讨:使用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中防止SQL注入最好的方法是什么
Jun 10 PHP
php中apc缓存使用示例
Dec 25 PHP
PHP常用的缓存技术汇总
May 05 PHP
ThinkPHP中Session用法详解
Nov 29 PHP
php可应用于面包屑导航的迭代寻找家谱树实现方法
Feb 02 PHP
详谈PHP编码转换问题
Jul 28 PHP
Yii中实现处理前后台登录的新方法
Dec 28 PHP
php+mysql+ajax实现单表多字段多关键词查询的方法
Apr 15 PHP
PHP依赖注入(DI)和控制反转(IoC)详解
Jun 12 PHP
php框架CodeIgniter使用redis的方法分析
Apr 13 PHP
实现php删除链表中重复的结点
Sep 27 PHP
浅谈PHP中的那些魔术常量
Dec 02 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
一个捕获函数输出的函数
2007/02/14 PHP
追求程序速度,而不是编程的速度
2008/04/23 PHP
codeigniter框架批量插入数据
2014/01/09 PHP
Joomla语言翻译类Jtext用法分析
2016/05/05 PHP
PHP+MySQL存储数据常见中文乱码问题小结
2016/06/13 PHP
键盘 keycode的值 javascript时触发事件时很有用的要素
2009/11/02 Javascript
为Extjs加加速(javascript加速)
2010/08/19 Javascript
window.onload和$(function(){})的区别介绍
2013/10/30 Javascript
深入理解JavaScript系列(29):设计模式之装饰者模式详解
2015/03/03 Javascript
jQuery实现当前页面标签高亮显示的方法
2015/03/10 Javascript
javascript操作select元素实例分析
2015/03/27 Javascript
jQuery纵向导航菜单效果实现方法
2016/12/19 Javascript
微信小程序  TLS 版本必须大于等于1.2问题解决
2017/02/22 Javascript
详解Node.js利用node-git-server快速搭建git服务器
2017/09/27 Javascript
vue.js的vue-cli脚手架中使用百度地图API的实例
2019/01/21 Javascript
Nodejs中的require函数的具体使用方法
2019/04/02 NodeJs
Vue中图片Src使用变量的方法
2019/10/30 Javascript
[10:39]DOTA2上海特级锦标赛音乐会纪录片
2016/03/21 DOTA
用Python实现协同过滤的教程
2015/04/08 Python
Python生成随机密码的方法
2017/06/16 Python
python中MethodType方法介绍与使用示例
2017/08/03 Python
Python调用C++,通过Pybind11制作Python接口
2018/10/16 Python
python 同时读取多个文件的例子
2019/07/16 Python
python中必要的名词解释
2019/11/20 Python
Python基于WordCloud制作词云图
2019/11/29 Python
基于Python模拟浏览器发送http请求
2020/11/06 Python
python中pow函数用法及功能说明
2020/12/04 Python
利用纯CSS3实现动态的自行车特效源码
2017/01/20 HTML / CSS
Champs Sports加拿大:北美最大的以商场为基础的专业运动鞋和服装零售商之一
2018/05/01 全球购物
小学领导班子对照材料
2014/08/23 职场文书
党员四风问题对照检查材料
2014/09/27 职场文书
2015年党风廉政承诺书
2015/01/22 职场文书
迎新晚会主持词开场白
2015/05/28 职场文书
解决golang post文件时Content-Type出现的问题
2021/05/02 Golang
使用Django实现商城验证码模块的方法
2021/06/01 Python
详解 TypeScript 枚举类型
2021/11/02 Javascript