Concept:XML serialization refers to the process of converting public fields and attributes into a sequence format (XML here) for storage or transmission. Deserialization re-creates the original state object from XML.
Copy codeThe Code is as follows:
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 );
}
}