无为清净楼资源网 Design By www.qnjia.com
概念: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);
}
}
复制代码 代码如下:
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);
}
}
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 孙露《一抹伤HQ》头版限量[WAV+CUE][1G]
- 黄安.1989-一切从头(TP版)【天际唱片】【FLAC分轨】
- 群星.1994-浓情蜜意情歌精丫华纳】【WAV+CUE】
- 邓丽君.1983-淡淡幽情(2022环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 试音天碟《专业测试第一天碟》经典天碟精选[WAV分轨][1G]
- 试音典范 《情惹发烧情HQCD》人声发烧极品 [WAV+CUE][1G]
- 世界顶级汽车音响试音王《幸福在路上》[低速原抓WAV+CUE][1.1G]
- 老头杯第二届什么时候开始 英雄联盟第二届老头杯开赛时间介绍
- 老头杯第二届什么时候结束 英雄联盟第二届老头杯结束时间介绍
- 老头杯第二届规则是什么 英雄联盟老头杯第二届规则介绍
- 王崴-爵士听堂.蓝色波萨(HQCD)[WAV+CUE]
- 群星《欧美动听情歌·柔情第5季》2CD【DTS-WAV分轨】
- [极品珍藏]德意志进行曲集卡拉扬SACD[WAV+CUE]
- 前暴雪制作人呼吁反击DEI 玩家:夺回文化!
- 腾讯证实子公司Sharkmob大规模裁员:整个市场很低迷