探讨:使用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);
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
网安追踪流程
1.不用代理网民发帖流程:发帖人 → ISP → 服务器托管商 → 服务器网警追踪流程:网监 → 服务器IP →...
[阅读全文]
-
-
-
HTTP状态码大全
转自:https://www.cnblogs.com/lxinxuan/archive/2009/10/22/1...
[阅读全文]
-
-
-
-
-
网友评论