static void Main() { var context = new GeographyEntities(); var document = XDocument.Load(@"../../rivers.xml"); var riversTag = document.XPathSelectElements(@"/rivers/river"); var rivers = new List<River>(); foreach (var xElement in riversTag) { rivers.Add(CreateRiver(xElement,context)); } context.Rivers.AddRange(rivers); context.SaveChanges(); }
private static River CreateRiver(XElement xElement, GeographyEntities context) { try { var name = xElement.Element("name").Value; var length = int.Parse(xElement.Element("length").Value); var outflow = xElement.Element("outflow").Value; int? drainageArea = null; if (xElement.Element("drainage-area") != null) { drainageArea = int.Parse(xElement.Element("drainage-area").Value); } int? averageDischarge = null; if (xElement.Element("average-discharge") != null) { averageDischarge = int.Parse(xElement.Element("average-discharge").Value); } var countriesNames = xElement.XPathSelectElements(@"countries/country"); var countriesPerRiver = countriesNames .Select(countryName => context.Countries .FirstOrDefault(c => c.CountryName == countryName.Value)) .ToList(); return new River() { RiverName = name, Length = length, Outflow = outflow, DrainageArea = drainageArea, AverageDischarge = averageDischarge, Countries = countriesPerRiver }; } catch (Exception) { throw; } }
static void Main() { var context = new GeographyEntities(); //Write a C# application based on your EF data model for exporting all monasteries by country to a XML file named monasteries.xml in the following XML format: var countries = context.Countries .Include(c => c.Monasteries) .Where(c => c.Monasteries.Any()) .Select(c => new { Name = c.CountryName, Monasteries = c.Monasteries .OrderBy(m => m.Name) .Select(m => m.Name) }) .OrderBy(c => c.Name) .ToList(); var document = new XDocument(); XElement countriesTag = new XElement("monasteries"); foreach (var country in countries) { XElement countryTag = new XElement("country"); countryTag.Add(new XAttribute("name", country.Name)); foreach (var monastery in country.Monasteries) { XElement monasteryTag = new XElement("monastery", monastery); countryTag.Add(monasteryTag); } countriesTag.Add(countryTag); } document.Add(countriesTag); document.Save(@"monasteries.xml"); System.Diagnostics.Process.Start(@"monasteries.xml"); }
static void Main() { var context = new GeographyEntities(); var rivers = context.Rivers .Where(r => r.Countries.Any()) .Select(r => new { r.RiverName, r.Length, Counties = r.Countries .OrderBy(c=>c.CountryName) .Select(c=>c.CountryName) }) .OrderByDescending(r=>r.Length) .ToList(); var riversAsJson = JsonConvert.SerializeObject(rivers,Formatting.Indented); File.WriteAllText(@"RiversJson.json",riversAsJson); System.Diagnostics.Process.Start(@"RiversJson.json"); }