一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 探討:使用XMLSerialize 序列化與反序列化

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

2020-04-10 15:41PHP教程網 PHP教程

本篇文章是對使用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);
        }
    }


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 垫底辣妹免费观看完整版 | 精品一区二区三区在线播放 | 国产成人精品福利色多多 | 能播放18xxx18女同 | bt天堂在线最新版在线 | 亚洲情射 | 闺蜜的样子小说安沁在线阅读 | www.男人天堂| 国产自拍专区 | 亚洲色图2 | 青青青国产精品国产精品久久久久 | 幻女free性摘花第一次 | 18捆绑调教在线高清 | 丁香五香天堂 | 亚洲国产欧美另类 | 国产99re在线观看69热 | 暖暖视频日本 | 草莓视频旧版本 | 国产主播精品在线 | 5151hh四虎国产精品 | 国产亚洲福利精品一区二区 | 午夜人妻理论片天堂影院 | 国产视频自拍一区 | 高清一级做a爱免费视 | 爆操美女 | 亚洲四虎 | 国产品精人成福利视频 | 欧美一区高清 | 男人的天堂久久爱 | 国产成人刺激视频在线观看 | 嫩草视频在线观看免费 | 9191精品国产观看 | 欧美一级级a在线观看 | 国产日韩欧美色视频色在线观看 | 99视频在线免费观看 | 久久AV国产麻豆HD真实乱 | 日本一区视频在线 | 欧美一区二区三区精品国产 | 叉逼视频| 国产一区二区精品久久91 | 天天干天天爽天天操 |