Serialization chuyển đối tượng thành byte stream để lưu vào bộ nhớ, file hoặc database.
Binary Serialization
Tất cả thành viên (kể cả read-only) đều được serialize.
XML Serialization
Chỉ serialize public fields/properties thành XML stream tuân theo XML Schema.
Ví dụ: thiết lập stream, tạo đối tượng, serialize, deserialize.
FileStream fstream = new FileStream("d:\\new.txt", FileMode.OpenOrCreate);
BinaryFormatter formatter = new BinaryFormatter();
Employee emp = new Employee(030, "Tom", "Operations");
formatter.Serialize(fStream, emp);
// Deserialize
formatter.Deserialize(fStream);