コード例 #1
0
        private static void ImportDiscoveries(SystemContext context)
        {
            var      xml         = XDocument.Load("../../Xml/discoveries.xml");
            XElement discoveries = xml.Root;

            foreach (var discovery in discoveries.Elements())
            {
                DateTime          dateMade      = DateTime.Parse(discovery.Attribute("DateMade").Value);
                List <Star>       stars         = discovery.Elements("Stars").Cast <Star>().ToList();
                List <Planet>     planets       = discovery.Elements("Planets").Cast <Planet>().ToList();
                List <Astronomer> pioneers      = discovery.Elements("Pioneers").Cast <Astronomer>().ToList();
                List <Astronomer> observers     = discovery.Elements("Observers").Cast <Astronomer>().ToList();
                string            telescopeUsed = discovery.Attribute("Telescope").Value;

                if (DM.HasNotExistingAstronomer(pioneers) || DM.HasNotExistingAstronomer(observers) ||
                    DM.HasNotExistingPlanet(planets) || DM.HasNotExistingStar(stars))
                {
                    continue;
                }

                Discovery dis = new Discovery()
                {
                    Date          = dateMade,
                    Stars         = stars,
                    Pioneers      = pioneers,
                    Observers     = observers,
                    Planets       = planets,
                    TelescopeUsed = DM.GetTelescope(telescopeUsed),
                };

                Console.WriteLine($"Successfully imported discovery.");
                context.Discoveries.Add(dis);
            }
            context.SaveChanges();
        }