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.theRadio.stationPresets = new double[] { 89.3, 105.1, 97.1 }; jbc.theRadio.hasTweeters = true; List<JamesBondCar> myCars = new List<JamesBondCar>(); myCars.Add(new JamesBondCar(true, true)); myCars.Add(new JamesBondCar(true, false)); myCars.Add(new JamesBondCar(false, false)); SavedAsXmlFormats(myCars, "CarCollection.xml"); // now save the car to a specific file in a binary format SavedAsXmlFormat(jbc, "CarData.xml"); BinaryFormatter binFormat = new BinaryFormatter(); // read the JamesBondCar From the binary file //using (Stream fStream = File.OpenRead("CarData.soap")) //{ // JamesBondCar carFromDis = (JamesBondCar)binFormat.Deserialize(fStream); // Console.WriteLine("Can this car fly? : {0}", carFromDis.canFly); //} Console.ReadLine(); }
private static void BinDeSerialize(string file) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"=> DeSerialize Using BinaryFormatter. Load from Binary File {file}"); BinaryFormatter bf = new BinaryFormatter(); using (Stream s = File.OpenRead(file)) { JamesBondCar c = (JamesBondCar)bf.Deserialize(s); Console.WriteLine($"Can this car fly? : {c.canFly}"); } }
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); } }
static void Main(string[] args) { JamesBondCar jbCar = new JamesBondCar(); jbCar.canFly = true; jbCar.isHatchBack = true; jbCar.theRadio.hasSubWoofers = true; jbCar.theRadio.stationPresets = new double[] { 99.7, 101.44 }; SaveAsBinaryFormat(jbCar, "CarData.dat"); SaveAsSoapFormat(jbCar, "CarDataSoap.xml"); SaveAsXMLFormat(jbCar, "CarData.xml"); SaveListOfCars("CarCollection.xml"); LoadFromBinaryFile("CarData.dat"); Console.ReadLine(); }
// 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 { CanFly = true, CanSubmerge = false, TheRadio = { StationPresets = new double[] { 89.3, 105.1, 97.1 }, HasTweeters = true } }; Person p = new Person { FirstName = "James", IsAlive = true }; // Now save the car to a specific file in a binary format. SaveAsBinaryFormat(jbc, "CarData.dat"); Console.WriteLine("=> Saved car in binary format!"); SaveAsBinaryFormat(p, "PersonData.dat"); Console.WriteLine("=> Saved person in binary format!"); JamesBondCar carFromDisk = LoadFromBinaryFile <JamesBondCar>("CarData.dat"); Console.WriteLine($"Can this car fly? : {carFromDisk.CanFly}"); Person personFromDisk = LoadFromBinaryFile <Person>("PersonData.dat"); Console.WriteLine($"Is {personFromDisk.FirstName} alive? : {personFromDisk.IsAlive}"); // XML SaveAsXmlFormat(jbc, "CarData.xml"); Console.WriteLine("=> Saved car in XML format!"); SaveAsXmlFormat(p, "PersonData.xml"); Console.WriteLine("=> Saved person in XML format!"); SaveListOfCarsAsXml(); SaveListOfCarsAsBinary(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Object Serialization *****\n"); JamesBondCar jamesBondCar = new JamesBondCar(); jamesBondCar.canFly = true; jamesBondCar.canSubmerge = false; jamesBondCar.theRadio.stationPresets = new double[] { 89.9, 107.7, 101.2 }; jamesBondCar.theRadio.hasTweeters = true; SaveAsBinaryFormat(jamesBondCar, "CarData.dat"); LoadFromBinaryFile("CarData.dat"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Object Serialization *****\n"); //Make a James BondCar 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 binary format SaveAsXmlFormat(jbc, "CarData.dat"); //Now load the car from specific file to object }
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"); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***Fun with Object Serialization ****"); StringData sd = new StringData(); SaveAsSoapFormat(sd, "sd.soap"); JamesBondCar jbc = new JamesBondCar(); jbc.canFly = true; jbc.canSubmerge = false; jbc.theRadio.stationPresets = new double[] { 89.3,105.3,97.1}; jbc.theRadio.hasTweeters = true; //SaveAsBinaryFormat(jbc, "jbc.txt"); //LoadFromBinaryFile(@"jbc.txt"); // SaveAsSoapFormat(jbc, "jbc.soap"); // LoadFromSoapFile(@"jbc.soap"); SaveAsXMLFormat(jbc, "jbc.xml"); LoadFromXMLFile(@"jbc.xml"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with Object Serialization ****"); //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 whichever format SaveAsBinaryFormat(jbc, "CarData.dat"); SaveAsSoapFormat(jbc, "SoapCarData.soap"); SaveAsXmlFormat(jbc, "XmlCarData.xml"); SaveListOfCar(); SaveListOfCarsAsBinary(); Console.Read(); }
private static JamesBondCar CreateJameBonderCar() { JamesBondCar jbc = new JamesBondCar(); jbc.canFly = true; jbc.canSubmerge = false; jbc.theRadio.stationPresets = new double[] { 89.3, 105.1, 97.1 }; jbc.theRadio.hasSubWoofers = true; StringBuilder b = new StringBuilder($"JamesBonderCar:\n canFly {jbc.canFly}\n canSubmerge {jbc.canSubmerge}\n Radio.stationPreset"); foreach (var f in jbc.theRadio.stationPresets) { b.Append(" ").Append(f); } b.Append($"\n Radio.hasSubWoofers {jbc.theRadio.hasSubWoofers}"); Console.WriteLine(b.ToString()); return(jbc); }
static void Main(string[] args) { Console.WriteLine("****** Fun with Object Serializable ******"); 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; SaveAsBinaryFormat(jbc, "CarData.dat"); ReadAsBinaryFormat("CarData.dat"); SaveAsSoapFormat(jbc, "CarData.dat"); ReadAsSoapFormat("CarData.dat"); SaveAsXML(jbc, "CarData.xml"); SaveListAsXML("CarDataList.xml"); Console.ReadLine(); }
// 确保使用了System.Runtime.Serializatiom.Formatters.Binary和System.IO命名空间 static void Main(string[] args) { Console.WriteLine("***** Fun with Object Serialization *****\n"); // 建立一个JamesBondCar并设定状态 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; // 将car以二进制歌手保存到指定文件中 SavaAsBinaryFormat(jbc, "CarData.dat"); LaodFromBinaryFile("CarData.dat"); SaveAsSoapFormat(jbc, "CarData.soap"); SaveAsXmlFormat(jbc, "CarData.xml"); SaveListOfCars(); SaveListOfCarsAsBinary(); Console.ReadLine(); }
static void Main(string[] args) { JamesBondCar jbc = new JamesBondCar(); ISerialize serializer; jbc.canFly = true; jbc.canSubmerge = false; jbc.theRadio.stationPresets = new double[] { 89.3, 105.1, 97.1 }; jbc.theRadio.hasTweeters = true; Console.WriteLine("Serialize to: "); Console.WriteLine("1. JSON"); Console.WriteLine("2. XML"); int input = 0; bool parsingResult = false; while (!parsingResult) { parsingResult = Int32.TryParse(Console.ReadLine(), out input); } switch (input) { case 1: serializer = new JsonSerializer(); break; case 2: serializer = new XMLSerializer <JamesBondCar>(); break; default: serializer = new JsonSerializer(); break; } string result = serializer.Serialize(jbc); Console.WriteLine("Result:"); Console.WriteLine(result); Console.ReadLine(); }
private static void Main(string[] args) { Console.WriteLine("***** Fun with Object Serialization\n"); var jbc = new JamesBondCar() { CanFly = true, CanSubmerge = false, TheRadio = new Radio() { stationPresets = new[] { 89.3, 105.1, 97.1 }, hasTweeters = true } }; // SaveAsBinaryFormat(jbc, "CarData.dat"); // LoadFromBinaryFile("CarData.dat"); // SaveAsSoapFormat(jbc, "CarData.dat"); // LoadFromSoapFile("CarData.dat"); // SaveAsXmlFormat(jbc, "CarData.dat"); JamesBondCar.SaveListOfCars(); Console.ReadLine(); }