Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Xml;
- namespace ConsoleApp1
- {
- // requirements for class which will be saved:
- // - class must be marked as [DataContract]
- // - all variables, which shall be saved, must be marked as [DataMember]
- [DataContract]
- public class Car
- {
- [DataMember]
- public string name;
- [DataMember]
- double power;
- [DataMember]
- List<Wheel> wheels;
- public Car(string name, double power, List<string> directions)
- {
- this.name = name;
- this.power = power;
- wheels = directions.Select(d => new Wheel(d)).ToList();
- }
- public override string ToString()
- {
- string returnString = name + " has " + power + "kW and those wheels: ";
- foreach (var wheel in wheels)
- returnString += wheel.direction + " ";
- return returnString;
- }
- }
- // requirements for subclasses of saved class:
- // - must have a public parameterless constructor
- public class Wheel
- {
- public string direction;
- public Wheel() { }
- public Wheel(string direction)
- {
- this.direction = direction;
- }
- }
- class Program
- {
- static void SaveViaDataContractSerialization<T>(T serializableObject, string filepath)
- {
- var serializer = new DataContractSerializer(typeof(T));
- var settings = new XmlWriterSettings()
- {
- Indent = true,
- IndentChars = "\t",
- };
- var writer = XmlWriter.Create(filepath, settings);
- serializer.WriteObject(writer, serializableObject);
- writer.Close();
- }
- static T LoadViaDataContractSerialization<T>(string filepath)
- {
- var fileStream = new FileStream(filepath, FileMode.Open);
- var reader = XmlDictionaryReader.CreateTextReader(fileStream, new XmlDictionaryReaderQuotas());
- var serializer = new DataContractSerializer(typeof(T));
- T serializableObject = (T)serializer.ReadObject(reader, true);
- reader.Close();
- fileStream.Close();
- return serializableObject;
- }
- static void Main(string[] args)
- {
- // Save single object
- Car bmw = new Car("BMW", 200, new List<string> { "Left", "Right" }); // create object
- SaveViaDataContractSerialization(bmw, "bmw.xml"); // save object
- bmw = null; // delete object
- bmw = LoadViaDataContractSerialization<Car>("bmw.xml"); // reload object
- Console.WriteLine(bmw.ToString()); // print object
- // Save list of objects
- List<Car> carList = new List<Car> // create object list
- {
- new Car("Porsche", 250, new List<string> { "Left" }),
- new Car("Mercedes", 150, new List<string> { "Front", "Back" }),
- new Car("Aston Martin", 300, new List<string> { "Front" })
- };
- SaveViaDataContractSerialization(carList, "cars.xml"); // save object list
- carList = null; // delete object list
- carList = LoadViaDataContractSerialization<List<Car>>("cars.xml"); // reload object list
- foreach (var a in carList) // print object list
- Console.WriteLine(a.ToString());
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement