一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

概念:XML序列化是將公共字段和屬性轉化為序列格式(這里指XML),以便存儲或傳輸?shù)倪^程。反序列化則是從XML中重新創(chuàng)建原始狀態(tài)的對象.
復制代碼 代碼如下:
    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技術探討:使用XMLSerialize 序列化與反序列化,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 黄色片在线观看网站 | 色草在线| 美女网站色黄 | 四虎国产永久免费久久 | 国产精品久久久久久久久鸭 | 国产欧美久久久另类精品 | 亚洲欧美一区二区三区四区 | 精品精品精品 | 99午夜视频| 亚洲一区二区三区四 | 亚洲第一伊人 | 国产在线播放91 | 亚洲伦理一二三四 | 97国产成人精品视频 | 国产欧美日韩免费一区二区 | 久久久久中文 | 国产亚洲欧美另类久久久 | 91福利视频一区 | 女人张腿给男人桶视频免费版 | 国产大片中文字幕在线观看 | 激情伊人网 | 91精品国产亚一区二区三区 | 亚洲国产精品线在线观看 | 色天天综合色天天碰 | 成人午夜免费视频毛片 | 男人扒开女人腿使劲桶动态图 | 九九视频在线看精品 | 91诱惑| 色综合久久综合欧美综合图片 | 亚洲第一免费网站 | 操吊视频| 啪啪91视频| 国产亚洲精品一品区99热 | 色综合五月激情综合色一区 | 91久久九九精品国产综合 | 色视频网站在线观看 | 久夜色精品国产一区二区三区 | 色视频一区 | 波多野结衣亚洲一区 | 久久丝袜精品中文字幕 | 色天天综合色天天碰 |