Ejemplo n.º 1
0
        public MainWindow()
        {
            InitializeComponent();

            dummyDatabase = new CarDatabase()
            {
                Cars = new List <Car>()
                {
                    new Car("1b1 111", "RR", "nevim"),
                    new Car("3z4 6151", "Skoda", "Rabia FS"),
                    new Car("1b1 1511", "Honda", "Civic Tourerr")
                }
            };
        }
Ejemplo n.º 2
0
        public static void SerializeToFile(string filePath, CarDatabase carDatabase)
        {
            if (carDatabase == null)
            {
                // Exception
                return;
            }

            if (File.Exists(filePath))
            {
                // Exception
                return;
            }

            using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(fileStream))
                {
                    writer.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                    writer.WriteLine("<CarDatabase>");
                    writer.WriteLine("<Cars>");
                    foreach (var car in carDatabase.Cars)
                    {
                        writer.WriteLine($"<Car>");

                        writer.WriteLine($"<Regno>{car.Regno}</Regno>");
                        writer.WriteLine($"<Manufacturer>{car.Manufacturer}</Manufacturer>");
                        writer.WriteLine($"<Type>{car.Type}</Type>");

                        writer.WriteLine($"</Car>");
                    }
                    writer.WriteLine($"</Cars>");
                    writer.WriteLine($"</CarDatabase>");
                }
            }
        }