探讨:使用XMLSerialize 序列化与反序列化
2015-01-24信息快讯网
本篇文章是对使用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); } }
linux实现php定时执行cron任务详解
php calender(日历)二个版本代码示例(解决2038问题)
php jquery 多文件上传简单实例
php配合jquery实现增删操作具体实例
Server.HTMLEncode让代码在页面里显示为源代码
浅析echo(),print(),print_r(),return之间的区别
PHP echo,print,printf,sprintf函数之间的区别与用法详解
php用header函数实现301跳转代码实例
php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence错误的处理方法
php5.3 不支持 session_register() 此函数已启用的解决方法
session在php5.3中的变化 session_is_registered() is deprecated in
PHP set_error_handler()函数使用详解(示例)
php生成图形(Libchart)实例
Linux编译升级php的详细方法
php cURL和Rolling cURL并发方式比较
FireFox浏览器使用Javascript上传大文件
php mysql_real_escape_string函数用法与实例教程
从PHP $_SERVER相关参数判断是否支持Rewrite模块
深入理解:XML与对象的序列化与反序列化
解析PHP自带的进位制之间的转换函数
PHP stripos()函数及注意事项的分析
coreseek 搜索英文的问题详解
使用session判断用户登录用户权限(超简单)
探讨PHP中this,self,parent的区别详解
深入php self与$this的详解
探讨php中header的用法详解
探讨如何把session存入数据库
解析php中call_user_func_array的作用
深入eAccelerator与memcached的区别详解
基于header的一些常用指令详解
深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念
基于php split()函数的用法详解
深入php list()函数的详解
php set_time_limit()函数的使用详解
浅谈php serialize()与unserialize()的用法
PHP CLI模式下的多进程应用分析
基于Discuz security.inc.php代码的深入分析
基于HBase Thrift接口的一些使用问题及相关注意事项的详解
php中serialize序列化与json性能测试的示例分析