private static void ImportDiscoveries() { XDocument documentXml = XDocument.Load(Constants.DiscoveriesPath); IEnumerable <XElement> discoveriesNode = documentXml.XPathSelectElements("Discoveries/Discovery"); using (PlanetHuntersContext context = new PlanetHuntersContext()) { foreach (XElement discoveryNode in discoveriesNode) { string dateMadeAsString = discoveryNode.Attribute("DateMade")?.Value; string telescopeNameAsString = discoveryNode.Attribute("Telescope")?.Value; bool ignoreDiscovery = false; if (dateMadeAsString == null) { Console.WriteLine(Messages.Error); continue; } DateTime dateMade = DateTime.Parse(dateMadeAsString); if (telescopeNameAsString == null) { Console.WriteLine(Messages.Error); continue; } if (!HelperMethods.IsTelescopeExisting(context, telescopeNameAsString)) { Console.WriteLine(Messages.Error); continue; } Telescope telescopeEntity = HelperMethods.GetTelescope(context, telescopeNameAsString); Discovery discoveryEntity = new Discovery() { DateMade = dateMade, TelesopeUsed = telescopeEntity }; IEnumerable <XElement> starsNode = discoveryNode.XPathSelectElements("Stars/Star"); foreach (XElement starNode in starsNode) { string starName = starNode.Value; if (!HelperMethods.IsStarExisting(context, starName)) { ignoreDiscovery = true; break; } discoveryEntity.Stars.Add(HelperMethods.GetStar(context, starName)); } if (ignoreDiscovery) { continue; } IEnumerable <XElement> planetsNode = discoveryNode.XPathSelectElements("Planets/Planet"); foreach (XElement planetNode in planetsNode) { string planetName = planetNode.Value; if (!HelperMethods.IsPlanetExisting(context, planetName)) { ignoreDiscovery = true; break; } discoveryEntity.Planets.Add(HelperMethods.GetPlanet(context, planetName)); } if (ignoreDiscovery) { continue; } IEnumerable <XElement> pioneersNode = discoveryNode.XPathSelectElements("Pioneers/Astronomer"); foreach (XElement pioneerNode in pioneersNode) { string pioneerName = pioneerNode.Value; string[] pionerNameArgs = pioneerName.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries); string firstName = pionerNameArgs[1]; string lastName = pionerNameArgs[0]; if (!HelperMethods.IsAstronomerExisting(context, firstName, lastName)) { ignoreDiscovery = true; break; } discoveryEntity.Pioneers.Add(HelperMethods.GetAstronomer(context, firstName, lastName)); } if (ignoreDiscovery) { continue; } IEnumerable <XElement> observersNode = discoveryNode.XPathSelectElements("Observers/Astronomer"); foreach (XElement observerNode in observersNode) { string observerName = observerNode.Value; string[] observerNameArgs = observerName.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries); string firstName = observerNameArgs[1]; string lastName = observerNameArgs[0]; if (!HelperMethods.IsAstronomerExisting(context, firstName, lastName)) { ignoreDiscovery = true; break; } discoveryEntity.Observers.Add(HelperMethods.GetAstronomer(context, firstName, lastName)); } if (ignoreDiscovery) { continue; } HelperMethods.AddDiscoveryToDatabase(context, discoveryEntity); } } }