static void LoadFromXmlFile(string fileName) { XmlSerializer xmlFormat = new XmlSerializer(typeof(JamesBondCar)); using (Stream fStream = File.OpenRead(fileName)) { JamesBondCar carFromDisk = (JamesBondCar)xmlFormat.Deserialize(fStream); Console.WriteLine("1. Can this car fly? : {0}", carFromDisk.canFly); Console.WriteLine("2. stationPresets of Radio object"); foreach (var stationPresets in carFromDisk.theRadio.stationPresets) { Console.WriteLine(stationPresets); } } }
static void LoadFromSoapFile(string fileName) { SoapFormatter soapFormat = new SoapFormatter(); // Read the JamesBondCar from the binary file. // It is stream in byte array(binary type data) using (Stream fStream = File.OpenRead(fileName)) { JamesBondCar carFromDisk = (JamesBondCar)soapFormat.Deserialize(fStream); Console.WriteLine("1. Can this car fly? : {0}", carFromDisk.canFly); Console.WriteLine("2. stationPresets of Radio object"); foreach (var stationPresets in carFromDisk.theRadio.stationPresets) { Console.WriteLine(stationPresets); } } }
static void LoadFromBinaryFile(string fileName) { BinaryFormatter binFormat = new BinaryFormatter(); // Read the JamesBondCar from the binary file. // It is stream in byte array(binary type data) using (Stream fStream = File.OpenRead(fileName)) { //I pass in that data(fStream) into Deserialize method to deserialze that binary data and to reconstruct object-graph which is return in object type so that I should cast object type to specific type that I want to use in explicitly JamesBondCar carFromDisk = (JamesBondCar)binFormat.Deserialize(fStream); //After deserializing binary data and then type casting object type to specific type explicitly, I can use object, extracting canFly property data Console.WriteLine("1. Can this car fly? : {0}", carFromDisk.canFly); Console.WriteLine("2. stationPresets of Radio object"); foreach (var stationPresets in carFromDisk.theRadio.stationPresets) { Console.WriteLine(stationPresets); } } }