探讨:使用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
屏蔽浏览器缓存另类方法
Oct 09 PHP
adodb与adodb_lite之比较
Dec 31 PHP
php中var_export与var_dump的区别分析
Aug 21 PHP
理解php原理的opcodes(操作码)
Oct 26 PHP
php中设置多级目录session的问题
Aug 08 PHP
php session劫持和防范的方法
Nov 12 PHP
php中数字、字符与对象判断函数用法实例
Nov 26 PHP
php对关联数组循环遍历的实现方法
Mar 13 PHP
PHP简单实现遍历目录下特定文件的方法小结
May 22 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 PHP
laravel7学习之无限级分类的最新实现方法
Sep 30 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
德劲1103二次变频版的打磨
2021/03/02 无线电
php5中date()得出的时间为什么不是当前时间的解决方法
2008/06/30 PHP
利用PHP+JS实现搜索自动提示(实例)
2013/06/09 PHP
老版本PHP转义Json里的特殊字符的函数
2015/06/08 PHP
laravel 创建命令行命令的图文教程
2019/10/23 PHP
跟随鼠标旋转的文字
2006/11/30 Javascript
url 特殊字符 传递参数解决方法
2010/01/01 Javascript
JSON.parse 解析字符串出错的解决方法
2010/07/08 Javascript
js 窗口抖动示例
2013/09/04 Javascript
使用JavaScript进行进制转换将字符串转换为十进制
2014/09/21 Javascript
js图片模糊切换显示特效的方法
2015/02/17 Javascript
AngularJS表格详解及示例代码
2016/08/17 Javascript
JavaScript变量作用域及内存问题实例分析
2019/06/10 Javascript
详解Vue.js中引入图片路径的几种方式
2019/06/17 Javascript
node.JS事件机制与events事件模块的使用方法详解
2020/02/06 Javascript
javascript局部自定义鼠标右键菜单
2020/12/08 Javascript
Python网络爬虫项目:内容提取器的定义
2016/10/25 Python
Python决策树分类算法学习
2017/12/22 Python
解决Ubuntu pip 安装 mysql-python包出错的问题
2018/06/11 Python
Python实现自定义函数的5种常见形式分析
2018/06/16 Python
python random从集合中随机选择元素的方法
2019/01/23 Python
set在python里的含义和用法
2019/06/24 Python
python字典嵌套字典的情况下找到某个key的value详解
2019/07/10 Python
Python OpenCV实现鼠标画框效果
2020/08/19 Python
python opencv 实现对图像边缘扩充
2020/01/19 Python
利用css3画个同心圆示例代码
2017/07/03 HTML / CSS
联想墨西哥官方网站:Lenovo墨西哥
2016/08/17 全球购物
天地会口号
2014/06/17 职场文书
飞机制造技术专业求职信
2014/07/27 职场文书
先进教师事迹材料
2014/12/16 职场文书
介绍长城的导游词
2015/01/30 职场文书
爱心捐赠活动简讯
2015/07/20 职场文书
导游词之山西祁县乔家大院
2019/10/14 职场文书
浅谈redis五大数据结构和使用场景
2021/04/12 Redis
浅谈redis整数集为什么不能降级
2021/07/25 Redis
MySQL中一条update语句是如何执行的
2022/03/16 MySQL