Discussion on the technique of using xmlserialize serialization and deserialization _php

Source: Internet
Author: User
Concept:XML serialization is the process of converting public fields and attributes into sequential format (which refers to XML) for storage or transmission. Deserialization is the object that recreated the original state from the XML.
Copy Code code 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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.