public static void Main() { var context = new GeographyEntities(); var monasteriesByCountry = context.Countries .Where(c => c.Monasteries.Any()) .OrderBy(c => c.CountryName) .Select(c => new { CountryName = c.CountryName, Monasteries = c.Monasteries .OrderBy(m => m.Name) .Select(m => m.Name) }); var xmlMonasteries = new XElement("monasteries"); foreach (var country in monasteriesByCountry) { var xmlCountry = new XElement("country"); xmlCountry.Add(new XAttribute("name", country.CountryName)); foreach (var monastery in country.Monasteries) { xmlCountry.Add(new XElement("monastery", monastery)); } xmlMonasteries.Add(xmlCountry); } var xmlDocument = new XDocument(xmlMonasteries); xmlDocument.Save(@"..\..\monasteries.xml"); Console.WriteLine("Exporting monasteries.xml complited!"); }
public static void Main() { var context = new GeographyEntities(); foreach (var continent in context.Continents) { Console.WriteLine(continent.ContinentName); } }
public static void Main() { var context = new GeographyEntities(); var rivers = context.Rivers .OrderByDescending(r => r.Length) .Select(r => new { riverName = r.RiverName, riverLenght = r.Length, countries = r.Countries .OrderBy(c => c.CountryName) .Select(c => c.CountryName) }); var js = new JavaScriptSerializer(); var riverJson = js.Serialize(rivers.ToList()); File.WriteAllText(@"..\..\rivers.json", riverJson); Console.WriteLine("Export rivers.json complited!"); }
public static void Main() { var context = new GeographyEntities(); var xmlDocument = XDocument.Load(@"..\..\rivers.xml"); var riverNodes = xmlDocument.XPathSelectElements("/rivers/river"); foreach (XElement riverNode in riverNodes) { try { if (riverNode.Element("name") == null) { throw new Exception("Name cannot be null."); } if (riverNode.Element("length") == null) { throw new Exception("Lenght cannot be null."); } if (riverNode.Element("outflow") == null) { throw new Exception("Outflow cannot be null."); } string riverName = riverNode.Element("name").Value; int riverLenght = int.Parse(riverNode.Element("length").Value); string riverOutflow = riverNode.Element("outflow").Value; int? drainageArea = null; if (riverNode.Element("drainage-area") != null) { drainageArea = int.Parse(riverNode.Element("drainage-area").Value); } int? averageDischarge = null; if (riverNode.Element("average-discharge") != null) { averageDischarge = int.Parse(riverNode.Element("average-discharge").Value); } var river = new River() { RiverName = riverName, Length = riverLenght, Outflow = riverOutflow, DrainageArea = drainageArea, AverageDischarge = averageDischarge }; IEnumerable<XElement> countryNodes = null; if (riverNode.Element("countries") != null) { countryNodes = riverNode.XPathSelectElements("/countries/country"); var countryNames = countryNodes.Select(c => c.Value); foreach (var countryName in countryNames) { var country = context.Countries.FirstOrDefault(c => c.CountryName == countryName); river.Countries.Add(country); } } context.Rivers.Add(river); context.SaveChanges(); Console.WriteLine("XML imported!"); } catch (Exception e) { Console.WriteLine(e.Message); } } }