亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

探討:使用XMLSerialize 序列化與反序列化

 更新時間:2013年06月08日 17:30:12   作者:  
本篇文章是對使用XMLSerialize 序列化與反序列化進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
概念:XML序列化是將公共字段和屬性轉(zhuǎn)化為序列格式(這里指XML),以便存儲或傳輸?shù)倪^程。反序列化則是從XML中重新創(chuàng)建原始狀態(tài)的對象.
復(fù)制代碼 代碼如下:

    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);
        }
    }

相關(guān)文章

  • php自動適應(yīng)范圍的分頁代碼

    php自動適應(yīng)范圍的分頁代碼

    分享一個自己寫的“頁碼自動適應(yīng)范圍”的分頁代碼
    2008-08-08
  • PHP獲取特殊時間戳的方法整理

    PHP獲取特殊時間戳的方法整理

    時間在我們?nèi)粘5拇a編寫中會是經(jīng)常出現(xiàn)的篩選或排序條件,尤其是一些特殊時間節(jié)點(diǎn)的時間顯得尤為突出。今天對部分相對簡便的方法進(jìn)行了部分整理,需要的可以參考一下
    2023-01-01
  • redirect_uri參數(shù)錯誤的解決方法(必看)

    redirect_uri參數(shù)錯誤的解決方法(必看)

    下面小編就為大家?guī)硪黄猺edirect_uri參數(shù)錯誤的解決方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • php allow_url_include的應(yīng)用和解釋

    php allow_url_include的應(yīng)用和解釋

    PHP常常因?yàn)樗赡茉试SURLS被導(dǎo)入和執(zhí)行語句被人們指責(zé)。事實(shí)上,這件事情并不是很讓人感到驚奇,因?yàn)檫@是導(dǎo)致稱為Remote URL Include vulnerabilities的php應(yīng)用程序漏洞的最重要的原因之一。
    2010-04-04
  • php自動跳轉(zhuǎn)中英文頁面

    php自動跳轉(zhuǎn)中英文頁面

    當(dāng)來訪者瀏覽器語言是中文就進(jìn)入中文版面 國外的用戶默認(rèn)瀏覽器不是中文的就跳轉(zhuǎn)英文頁面
    2008-07-07
  • Eclipse的PHP插件PHPEclipse安裝和使用

    Eclipse的PHP插件PHPEclipse安裝和使用

    PHP有很多相當(dāng)不錯的開發(fā)工具,如Zend Studio、NetBeans、phpdesigner等,但對于習(xí)慣Java編程的程序猿們來說,最常用的還要屬Eclipse。那么Eclipse能用于PHP開發(fā)嗎?答案是“必須滴”。
    2014-07-07
  • PHP設(shè)計(jì)模式之委托模式定義與用法簡單示例

    PHP設(shè)計(jì)模式之委托模式定義與用法簡單示例

    這篇文章主要介紹了PHP設(shè)計(jì)模式之委托模式定義與用法,簡單描述了委托模式的功能、定義與簡單使用方法,需要的朋友可以參考下
    2018-08-08
  • PHP+MYSQL中文亂碼問題

    PHP+MYSQL中文亂碼問題

    這篇文章主要匯總介紹了幾種解決PHP+MYSQL中文亂碼問題的方法,十分的實(shí)用,有需要的小伙伴可以參考下。
    2015-07-07
  • PHP pthreads v3下的Volatile簡介與使用方法示例

    PHP pthreads v3下的Volatile簡介與使用方法示例

    這篇文章主要介紹了PHP pthreads v3下的Volatile簡介與使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP pthreads v3下Volatile的功能、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • PHP編碼規(guī)范的深入探討

    PHP編碼規(guī)范的深入探討

    本篇文章是對PHP編碼規(guī)范進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論