Beispiel #1
0
        public static void SerializeXml(CarPark park)
        {
            string path         = @"D:\\lol.xml";
            var    serializer   = new XmlSerializer(typeof(CarPark));
            var    encoding     = Encoding.GetEncoding("utf-8");
            var    namespaceStr = "http://lol.by/catalog";

            XmlSerializerNamespaces defaultNamespace = new XmlSerializerNamespaces();

            defaultNamespace.Add(string.Empty, namespaceStr);

            using (StreamWriter writer = new StreamWriter(path, false, encoding))
            {
                serializer.Serialize(writer, park, defaultNamespace);
            }
        }
Beispiel #2
0
        private CarPark GetDefaultCarPark()
        {
            var carPark = new CarPark();

            carPark.Cars.Add(new LightCar(1, "BMW", 2011, 6.3m, 18000, 4));
            carPark.Cars.Add(new LightCar(2, "BMW", 2016, 4.7m, 2800, 4));
            carPark.Cars.Add(new LightCar(3, "Audi", 2008, 4.0m, 12000, 4));
            carPark.Cars.Add(new LightCar(4, "Renault", 2014, 3.7m, 1200, 4));
            carPark.Cars.Add(new LightCar(5, "Lada", 2016, 4.2m, 12000, 4));
            carPark.Cars.Add(new LightCar(6, "Volkswagen", 2004, 8m, 19000, 8));
            carPark.Cars.Add(new LightCar(7, "Lada", 2002, 6.3m, 6000, 4));
            carPark.Cars.Add(new LightCar(8, "Volkswagen", 2011, 6.3m, 18000, 4));
            carPark.Cars.Add(new LightCar(9, "Renault", 2011, 6.3m, 18000, 4));
            carPark.Cars.Add(new LightCar(10, "Volvo", 2016, 6.3m, 23000, 4));
            carPark.Cars.Add(new CargoCar(11, "BMW", 2011, 6.3m, 18000, 1200));
            carPark.Cars.Add(new CargoCar(12, "Volvo", 2010, 8m, 14000, 3800));
            carPark.Cars.Add(new CargoCar(13, "Mercedes", 2012, 5.6m, 16500, 3200));

            return(carPark);
        }
Beispiel #3
0
        public static CarPark DeserializeXml()
        {
            CarPark park         = null;
            string  path         = @"D:\\lol.xml";
            var     serializer   = new XmlSerializer(typeof(CarPark));
            var     encoding     = Encoding.GetEncoding("utf-8");
            var     namespaceStr = "http://lol.by/catalog";

            XmlSerializerNamespaces defaultNamespace = new XmlSerializerNamespaces();

            defaultNamespace.Add(string.Empty, namespaceStr);


            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                var loadedObject = serializer.Deserialize(stream);

                if (loadedObject is CarPark)
                {
                    park = (CarPark)loadedObject;
                }
            }
            return(park);
        }