public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string file = @"c:\tmp\saleitem.bin"; BinarySerialize(file); XmlSerialize(file+".xml"); } private void XmlSerialize(string file) { string[] shoes = { "Brogue", "Plim", "Boot" }; { SaleItem it = new SaleItem("Shoes", 49m); se.WriteToXmlFile(file, it); } Random r = new Random(); List items = new List(); for (int i = 0; i < 10; i++) { items.Add(new SaleItem(shoes[i % shoes.Length], r.Next(5, 11))); } se.WriteToXmlFile>(file, items); List its = se.ReadFromXmlFile>(file); its.ForEach(i => Console.WriteLine("{0}", i.ToString())); } private void BinarySerialize(string file) { string[] shoes = { "Brogue", "Plim", "Boot" }; { SaleItem it = new SaleItem("Shoes", 49m); se.WriteToBinaryFile(file, it); } Random r = new Random(); List items = new List(); for (int i = 0; i < 10; i++) { items.Add(new SaleItem(shoes[i % shoes.Length], r.Next(5, 11))); } se.WriteToBinaryFile>(file, items); List its = se.ReadFromBinaryFile>(file); its.ForEach(i => Console.WriteLine("{0}", i.ToString())); int y = 0; } } public static class se { //public se() { } /// /// Writes the given object instance to a binary file. /// Object type (and all child types) must be decorated with the [Serializable] attribute. /// To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties. /// /// The type of object being written to the binary file. /// The file path to write the object instance to. /// The object instance to write to the binary file. /// If false the file will be overwritten if it already exists. If true the contents will be appended to the file. public static void WriteToBinaryFile(string filePath, T objectToWrite, bool append = false) { using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); binaryFormatter.Serialize(stream, objectToWrite); } } /// /// Reads an object instance from a binary file. /// /// The type of object to read from the binary file. /// The file path to read the object instance from. /// Returns a new instance of the object read from the binary file. public static T ReadFromBinaryFile(string filePath) { using (Stream stream = File.Open(filePath, FileMode.Open)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); return (T)binaryFormatter.Deserialize(stream); } } /// /// Writes the given object instance to an XML file. /// Only Public properties and variables will be written to the file. These can be any type though, even other classes. /// If there are public properties/variables that you do not want written to the file, decorate them with the [XmlIgnore] attribute. /// Object type must have a parameterless constructor. /// /// The type of object being written to the file. /// The file path to write the object instance to. /// The object instance to write to the file. /// If false the file will be overwritten if it already exists. If true the contents will be appended to the file. public static void WriteToXmlFile(string filePath, T objectToWrite, bool append = false) where T : new() { TextWriter writer = null; try { var serializer = new XmlSerializer(typeof(T)); writer = new StreamWriter(filePath, append); serializer.Serialize(writer, objectToWrite); } finally { if (writer != null) writer.Close(); } } /// /// Reads an object instance from an XML file. /// Object type must have a parameterless constructor. /// /// The type of object to read from the file. /// The file path to read the object instance from. /// Returns a new instance of the object read from the XML file. public static T ReadFromXmlFile(string filePath) where T : new() { TextReader reader = null; try { var serializer = new XmlSerializer(typeof(T)); reader = new StreamReader(filePath); return (T)serializer.Deserialize(reader); } finally { if (reader != null) reader.Close(); } } } [Serializable] public class SaleItem { public SaleItem() { } string _name; decimal _cost; public SaleItem(string name, decimal cost) { _name = name; _cost = cost; } public string Name { get => _name; set => _name = value; } public decimal Price { get => _cost; set => _cost = value; } public void Dump() { Console.WriteLine(Name); Console.WriteLine(Price); } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0} -> {1}", Name, Price); return sb.ToString(); } }