static void Main(string[] args) { Predator tiger = new Predator("Africa", "Tiger", 15); Predator Lion = new Predator("Africa", "Lion", 10); Predator Wolf = new Predator("Europe", "Wolf", 2); Animals[] animals = new Animals[] { tiger, Lion, Wolf }; foreach (object a in animals) { //Console.WriteLine(a.ToString); } BinarySerialize(tiger); BinaryDeserialize(); JsonSerialize(Lion); JsonDeserialize(); SOAPSerialize(Wolf); SOAPDeserialize(); XMLSerialize(tiger); XMLDeserialize(); //Array_JsonSerialize(animals); //Array_JsonDeserialize(); Array_XMLSerialize(animals); //Array_XMLDeserialize(); XPath(); XmlLinq(); }
static void BinaryDeserialize() { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream("animals.dat", FileMode.OpenOrCreate)) { Predator newpredator = (Predator)formatter.Deserialize(fs); Console.WriteLine($"{newpredator.predator}\n Название : {newpredator.name } Место обитания : {newpredator.Habitat} Возраст: {newpredator.age} "); Console.WriteLine("Объект десериализован binary"); } }
static void XMLDeserialize() { XmlSerializer deserialize = new XmlSerializer(typeof(Predator)); using (FileStream fs = new FileStream("predator.xml", FileMode.OpenOrCreate)) { Predator predator = (Predator)deserialize.Deserialize(fs); predator.Show(); Console.WriteLine("XML десериализован"); } }
static void SOAPDeserialize() { SoapFormatter formatter = new SoapFormatter(); using (FileStream fs = new FileStream("predator.soap", FileMode.OpenOrCreate)) { Predator predators = (Predator)formatter.Deserialize(fs); Console.WriteLine("Десериализован SOAP"); predators.Show(); } }
static void JsonDeserialize() { DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Predator)); using (FileStream fs = new FileStream("predator.json", FileMode.OpenOrCreate)) { Predator predator = (Predator)json.ReadObject(fs); predator.Show(); Console.WriteLine("Объект десериализован Json"); } }