static void LoadFromSOAPFile(string fileName) { SoapFormatter soapFormat = new SoapFormatter(); // Read the JamesBondCar from the binary file. using (Stream fStream = File.OpenRead(fileName)) { JamesBondCar carFromDisk = (JamesBondCar)soapFormat.Deserialize(fStream); Console.WriteLine("Can this car fly? : {0}", carFromDisk.canFly); } }
static void LoadFromXMLFile(string fileName) { XmlSerializer xmlFormat = new XmlSerializer(typeof(JamesBondCar)); // Read the JamesBondCar from the binary file. using (Stream fStream = File.OpenRead(fileName)) { JamesBondCar carFromDisk = (JamesBondCar)xmlFormat.Deserialize(fStream); Console.WriteLine("Can this car fly? : {0}", carFromDisk.canFly); } }
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"); SaveAsSoapFormat(jbc, "CarData.soap"); LoadFromSOAPFile("CarData.soap"); SaveAsXmlFormat(jbc, "CarData.xml"); LoadFromXMLFile("CarData.xml"); Console.ReadLine(); }