Advertisement
Guest User

datacontract

a guest
Jun 1st, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Xml;
  7.  
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     // requirements for class which will be saved:
  12.      //    - class must be marked as [DataContract]
  13.      //    - all variables, which shall be saved, must be marked as [DataMember]
  14.  
  15.     [DataContract]
  16.     public class Car
  17.     {
  18.         [DataMember]
  19.         public string name;
  20.  
  21.         [DataMember]
  22.         double power;
  23.  
  24.         [DataMember]
  25.         List<Wheel> wheels;
  26.  
  27.         public Car(string name, double power, List<string> directions)
  28.         {
  29.             this.name = name;
  30.             this.power = power;
  31.             wheels = directions.Select(d => new Wheel(d)).ToList();
  32.         }
  33.  
  34.         public override string ToString()
  35.         {
  36.             string returnString = name + " has " + power + "kW and those wheels: ";
  37.             foreach (var wheel in wheels)
  38.                 returnString += wheel.direction + " ";
  39.             return returnString;
  40.         }
  41.     }
  42.  
  43.     // requirements for subclasses of saved class:
  44.     //    - must have a public parameterless constructor
  45.  
  46.     public class Wheel
  47.     {
  48.         public string direction;
  49.  
  50.         public Wheel() { }
  51.  
  52.         public Wheel(string direction)
  53.         {
  54.             this.direction = direction;
  55.         }
  56.     }
  57.  
  58.     class Program
  59.     {
  60.         static void SaveViaDataContractSerialization<T>(T serializableObject, string filepath)
  61.         {
  62.             var serializer = new DataContractSerializer(typeof(T));
  63.             var settings = new XmlWriterSettings()
  64.             {
  65.                 Indent = true,
  66.                 IndentChars = "\t",
  67.             };
  68.             var writer = XmlWriter.Create(filepath, settings);
  69.             serializer.WriteObject(writer, serializableObject);
  70.             writer.Close();
  71.         }
  72.  
  73.  
  74.         static T LoadViaDataContractSerialization<T>(string filepath)
  75.         {
  76.             var fileStream = new FileStream(filepath, FileMode.Open);
  77.             var reader = XmlDictionaryReader.CreateTextReader(fileStream, new XmlDictionaryReaderQuotas());
  78.             var serializer = new DataContractSerializer(typeof(T));
  79.             T serializableObject = (T)serializer.ReadObject(reader, true);
  80.             reader.Close();
  81.             fileStream.Close();
  82.             return serializableObject;
  83.         }
  84.  
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             // Save single object
  89.             Car bmw = new Car("BMW", 200, new List<string> { "Left", "Right" });    // create object
  90.             SaveViaDataContractSerialization(bmw, "bmw.xml");                       // save object
  91.             bmw = null;                                                             // delete object
  92.             bmw = LoadViaDataContractSerialization<Car>("bmw.xml");                 // reload object
  93.             Console.WriteLine(bmw.ToString());                                      // print object
  94.  
  95.  
  96.            
  97.             // Save list of objects
  98.             List<Car> carList = new List<Car>                                       // create object list
  99.             {
  100.                 new Car("Porsche", 250, new List<string> { "Left" }),
  101.                 new Car("Mercedes", 150, new List<string> { "Front", "Back" }),
  102.                 new Car("Aston Martin", 300, new List<string> { "Front" })
  103.             };
  104.             SaveViaDataContractSerialization(carList, "cars.xml");                  // save object list
  105.             carList = null;                                                         // delete object list
  106.             carList = LoadViaDataContractSerialization<List<Car>>("cars.xml");      // reload object list
  107.             foreach (var a in carList)                                              // print object list
  108.                 Console.WriteLine(a.ToString());
  109.                
  110.             Console.ReadLine();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement