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

 更新时间:2013年06月08日 17:30:12   作者:  
本篇文章是对使用XMLSerialize 序列化与反序列化进行了详细的分析介绍,需要的朋友参考下
概念: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自动适应范围的分页代码

    分享一个自己写的“页码自动适应范围”的分页代码
    2008-08-08
  • PHP获取特殊时间戳的方法整理

    PHP获取特殊时间戳的方法整理

    时间在我们日常的代码编写中会是经常出现的筛选或排序条件,尤其是一些特殊时间节点的时间显得尤为突出。今天对部分相对简便的方法进行了部分整理,需要的可以参考一下
    2023-01-01
  • redirect_uri参数错误的解决方法(必看)

    redirect_uri参数错误的解决方法(必看)

    下面小编就为大家带来一篇redirect_uri参数错误的解决方法(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • php allow_url_include的应用和解释

    php allow_url_include的应用和解释

    PHP常常因为它可能允许URLS被导入和执行语句被人们指责。事实上,这件事情并不是很让人感到惊奇,因为这是导致称为Remote URL Include vulnerabilities的php应用程序漏洞的最重要的原因之一。
    2010-04-04
  • php自动跳转中英文页面

    php自动跳转中英文页面

    当来访者浏览器语言是中文就进入中文版面 国外的用户默认浏览器不是中文的就跳转英文页面
    2008-07-07
  • Eclipse的PHP插件PHPEclipse安装和使用

    Eclipse的PHP插件PHPEclipse安装和使用

    PHP有很多相当不错的开发工具,如Zend Studio、NetBeans、phpdesigner等,但对于习惯Java编程的程序猿们来说,最常用的还要属Eclipse。那么Eclipse能用于PHP开发吗?答案是“必须滴”。
    2014-07-07
  • PHP设计模式之委托模式定义与用法简单示例

    PHP设计模式之委托模式定义与用法简单示例

    这篇文章主要介绍了PHP设计模式之委托模式定义与用法,简单描述了委托模式的功能、定义与简单使用方法,需要的朋友可以参考下
    2018-08-08
  • PHP+MYSQL中文乱码问题

    PHP+MYSQL中文乱码问题

    这篇文章主要汇总介绍了几种解决PHP+MYSQL中文乱码问题的方法,十分的实用,有需要的小伙伴可以参考下。
    2015-07-07
  • PHP pthreads v3下的Volatile简介与使用方法示例

    PHP pthreads v3下的Volatile简介与使用方法示例

    这篇文章主要介绍了PHP pthreads v3下的Volatile简介与使用方法,结合实例形式较为详细的分析了PHP pthreads v3下Volatile的功能、原理、使用方法及相关操作注意事项,需要的朋友可以参考下
    2020-02-02
  • PHP编码规范的深入探讨

    PHP编码规范的深入探讨

    本篇文章是对PHP编码规范进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论