gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章探讨:使用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);         }     } 。


最后此篇关于探讨:使用XMLSerialize 序列化与反序列化的文章就讲到这里了,如果你想了解更多关于探讨:使用XMLSerialize 序列化与反序列化的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com