// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace E { [Serializable] public class Data { private int v1; private int v2; public Data() { v1 = 1; v2 = 2; } public void IncV1() { v1++; } public int V2 { get { return v2; } set { v2 = value; } } public override string ToString() { return "(" + v1 + "," + v2 + ")"; } } public class Program { public static void Main(string[] args) { Data o = new Data(); o.IncV1(); o.V2 = 3; Console.WriteLine(o); XmlSerializer ser = new XmlSerializer(typeof(Data)); StreamWriter sw = new StreamWriter(@"C:\data.xml"); ser.Serialize(sw, o); sw.Close(); StreamReader sr = new StreamReader(@"C:\data.xml"); Data ox = (Data)ser.Deserialize(sr); sr.Close(); Console.WriteLine(ox); BinaryFormatter bf = new BinaryFormatter(); FileStream outf = File.Create(@"C:\data.bin"); bf.Serialize(outf, o); outf.Close(); FileStream inf = File.OpenRead(@"C:\data.bin"); Data ob = (Data)bf.Deserialize(inf); inf.Close(); Console.WriteLine(ob); } } }