Example from Troelsen
see methods
// XML
SaveAsXmlFormat(jbc, “CarData.xml”);
SaveListOfCars();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // Gain access to the BinaryFormatter in mscorlib.dll. using System.Runtime.Serialization.Formatters.Binary; // Must reference System.Runtime.Serialization.Formatters.Soap.dll. using System.Runtime.Serialization.Formatters.Soap; // Defined within System.Xml.dll. using System.Xml.Serialization; using System.IO; namespace SimpleSerialize { #region Types to serialize [Serializable] public class Radio { public bool hasTweeters; public bool hasSubWoofers; public double[] stationPresets; [NonSerialized] public string radioID = "XF-552RR6"; } [Serializable] public class Car { public Radio theRadio = new Radio(); public bool isHatchBack; } [Serializable] [XmlRoot(Namespace = "http://www.MyCompany.com")] public class JamesBondCar : Car { [XmlAttribute] public bool canFly; [XmlAttribute] public bool canSubmerge; public JamesBondCar(bool skyWorthy, bool seaWorthy) { canFly = skyWorthy; canSubmerge = seaWorthy; } // The XmlSerializer demands a default constructor! public JamesBondCar() { } } #endregion class Program { // Be sure to import the System.Runtime.Serialization.Formatters.Binary // and System.IO namespaces. static void Main(string[] args) { Console.WriteLine("***** Fun with Object Serialization *****\n"); // Make a JamesBondCar and set state. JamesBondCar jbc = new JamesBondCar(); jbc.canFly = true; jbc.canSubmerge = false; jbc.theRadio.stationPresets = new double[] { 89.3, 105.1, 97.1 }; jbc.theRadio.hasTweeters = true; // Now save the car to a specific file in a binary format. SaveAsBinaryFormat(jbc, "CarData.dat"); LoadFromBinaryFile("CarData.dat"); // Save as SOAP. SaveAsSoapFormat(jbc, "CarData.soap"); // XML SaveAsXmlFormat(jbc, "CarData.xml"); SaveListOfCars(); Console.ReadLine(); } #region Load from binary static void LoadFromBinaryFile(string fileName) { BinaryFormatter binFormat = new BinaryFormatter(); // Read the JamesBondCar from the binary file. using (Stream fStream = File.OpenRead(fileName)) { JamesBondCar carFromDisk = (JamesBondCar)binFormat.Deserialize(fStream); Console.WriteLine("Can this car fly? : {0}", carFromDisk.canFly); } } #endregion #region Save as binary static void SaveAsBinaryFormat(object objGraph, string fileName) { // Save object to a file named CarData.dat in binary. BinaryFormatter binFormat = new BinaryFormatter(); using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { binFormat.Serialize(fStream, objGraph); } Console.WriteLine("=> Saved car in binary format!"); } #endregion #region Save as SOAP // Be sure to import System.Runtime.Serialization.Formatters.Soap // and reference System.Runtime.Serialization.Formatters.Soap.dll. static void SaveAsSoapFormat(object objGraph, string fileName) { // Save object to a file named CarData.soap in SOAP format. SoapFormatter soapFormat = new SoapFormatter(); using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, objGraph); } Console.WriteLine("=> Saved car in SOAP format!"); } #endregion #region Save as XML static void SaveAsXmlFormat(object objGraph, string fileName) { // Save object to a file named CarData.xml in XML format. XmlSerializer xmlFormat = new XmlSerializer(typeof(JamesBondCar)); using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { xmlFormat.Serialize(fStream, objGraph); } Console.WriteLine("=> Saved car in XML format!"); } #endregion #region Save LIST of objects static void SaveListOfCars() { // Now persist a List<T> of JamesBondCars. List<JamesBondCar> myCars = new List<JamesBondCar>(); myCars.Add(new JamesBondCar(true, true)); myCars.Add(new JamesBondCar(true, false)); myCars.Add(new JamesBondCar(false, true)); myCars.Add(new JamesBondCar(false, false)); using (Stream fStream = new FileStream("CarCollection.xml", FileMode.Create, FileAccess.Write, FileShare.None)) { XmlSerializer xmlFormat = new XmlSerializer(typeof(List<JamesBondCar>)); xmlFormat.Serialize(fStream, myCars); } Console.WriteLine("=> Saved list of cars!"); } static void SaveListOfCarsAsBinary() { // Save ArrayList object (myCars) as binary. List<JamesBondCar> myCars = new List<JamesBondCar>(); BinaryFormatter binFormat = new BinaryFormatter(); using (Stream fStream = new FileStream("AllMyCars.dat", FileMode.Create, FileAccess.Write, FileShare.None)) { binFormat.Serialize(fStream, myCars); } Console.WriteLine("=> Saved list of cars in binary!"); } #endregion } } |