您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何使用XMLSerialize序列化與反序列化的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
概念:XML序列化是將公共字段和屬性轉(zhuǎn)化為序列格式(這里指XML),以便存儲或傳輸?shù)倪^程。反序列化則是從XML中重新創(chuàng)建原始狀態(tài)的對象.
代碼如下:
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);
}
}
感謝各位的閱讀!關(guān)于“如何使用XMLSerialize序列化與反序列化”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。