欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2013年06月08日 17:30:12   作者:  
本篇文章是對(duì)使用XMLSerialize 序列化與反序列化進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
概念:XML序列化是將公共字段和屬性轉(zhuǎn)化為序列格式(這里指XML),以便存儲(chǔ)或傳輸?shù)倪^(guò)程。反序列化則是從XML中重新創(chuàng)建原始狀態(tài)的對(duì)象.
復(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自動(dòng)適應(yīng)范圍的分頁(yè)代碼

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

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

    PHP獲取特殊時(shí)間戳的方法整理

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

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

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

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

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

    php自動(dòng)跳轉(zhuǎn)中英文頁(yè)面

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

    Eclipse的PHP插件PHPEclipse安裝和使用

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

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

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

    PHP+MYSQL中文亂碼問(wèn)題

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

    PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法示例

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

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

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

最新評(píng)論