Advertisement
MaksNew

Untitled

Jan 19th, 2022
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6.  
  7. namespace Lightpoint_demo2
  8. {
  9.     public class XMLConverter: ISerializer<Office>
  10.     {
  11.         private const string filename = "cars.xml";
  12.         private readonly XmlSerializer serializer;  
  13.         private List<Office> Offices { get; set; }  
  14.         public XMLConverter(List<Office> offices)
  15.         {
  16.             serializer = new XmlSerializer(typeof(SerializableMOffice));
  17.             Offices = offices;  
  18.         }
  19.         public void Serialize()
  20.         {
  21.             using (FileStream fs = new FileStream(filename, FileMode.Create))
  22.             {
  23.                 SerializableMOffice s_moffice = new SerializableMOffice();
  24.                 for (int i = 0; i < Offices.Count; i++)
  25.                 {
  26.                     Offices[i].SerialazibleOffice.Brand = Offices[i].Brand;
  27.                     Offices[i].SerialazibleOffice.SerializableCarsList = Offices[i].CarsDataBase.GetItemsList();
  28.                     s_moffice.SerializableOfficeList.Add(Offices[i].SerialazibleOffice);
  29.                 }
  30.                 serializer.Serialize(fs, s_moffice);
  31.             }
  32.         }
  33.         public void Deserialize()
  34.         {
  35.             using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
  36.             {
  37.                 Office office;
  38.                 SerializableMOffice s_moffice;
  39.                 try
  40.                 {
  41.                     s_moffice = (SerializableMOffice)serializer.Deserialize(fs);
  42.                 }
  43.                 catch (Exception)
  44.                 {
  45.                     return;
  46.                 }
  47.                 for (int i = 0; i < s_moffice.SerializableOfficeList.Count; i++)
  48.                 {
  49.                     office = new Office(s_moffice.SerializableOfficeList[i].Brand, "XML");
  50.                     office.CarsDataBase = new LocalDB();
  51.                     for (int j = 0; j < s_moffice.SerializableOfficeList[i].SerializableCarsList.Count; j++)
  52.                     {
  53.                         office.CarsDataBase.Create(s_moffice.SerializableOfficeList[i].SerializableCarsList[j]);
  54.                     }
  55.                     Offices.Add(office);
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement