探讨:使用XMLSerialize 序列化与反序列化
程序员文章站
2023-02-23 21:36:20
概念:xml序列化是将公共字段和属性转化为序列格式(这里指xml),以便存储或传输的过程。反序列化则是从xml中重新创建原始状态的对象.复制代码 代码如下: &n...
概念: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);
}
}
推荐阅读
-
Python开发之序列化与反序列化:pickle、json模块使用详解
-
C# 关于使用JavaScriptSerializer 序列化与返序列化的操作
-
探讨:使用XMLSerialize 序列化与反序列化
-
.NET Core中使用Redis与Memcached的序列化问题详析
-
关于.NetCore与.Netframework 对于DataSet的序列化与反序列化问题的探讨.
-
C# xml序列化与反序列化 特性的使用
-
详解Python中的序列化与反序列化的使用
-
EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]
-
探讨:使用XMLSerialize 序列化与反序列化
-
Python开发之序列化与反序列化:pickle、json模块使用详解