예제 #1
0
        static void Main(string[] args)
        {
            // Console.WriteLine(new GeographyEntities().Rivers.Count());
            var xmlDoc     = XDocument.Load(@"..\..\rivers.xml");
            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                string riverName    = riverNode.Element("name").Value;
                int    riverLength  = 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);
                }
                Console.WriteLine("{0} {1} {2} {3} {4}", riverName, riverLength, riverOutflow, drainageArea, averageDischarge);
                var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countries    = countryNodes.Select(c => c.Value);
                Console.WriteLine("{0} -> {1}", riverName, string.Join(", ", countries));

                var context = new GeographyEntities();
                var river   = new River()
                {
                    RiverName        = riverName,
                    Length           = riverLength,
                    Outflow          = riverOutflow,
                    DrainageArea     = drainageArea,
                    AverageDischarge = averageDischarge
                };
                context.Rivers.Add(river);
                context.SaveChanges();

                //var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countrNames = countryNodes.Select(c => c.Value);
                foreach (var countryName in countrNames)
                {
                    var country = context.Countries
                                  .FirstOrDefault(c => c.CountryName == countryName);
                    river.Countries.Add(country);
                }
                context.SaveChanges();
            }
        }
예제 #2
0
        static void Main()
        {
            var context = new GeographyEntities();
            var xmlDoc  = XDocument.Load(@"..\..\rivers.xml");
            //Console.WriteLine(xmlDoc);
            var riverElements = xmlDoc.Root.Elements();

            foreach (var riverElement in riverElements)  //in xmlDoc.XPathSelectElements("/rivers/river")
            {
                var riverEntity = new River();
                riverEntity.RiverName = riverElement.Element("name").Value;
                //Console.WriteLine(riverElement.Element("name").Value);
                riverEntity.Length  = int.Parse(riverElement.Element("length").Value);
                riverEntity.Outflow = riverElement.Element("outflow").Value;
                if (riverElement.Element("drainage-area") != null)
                {
                    riverEntity.DrainageArea = int.Parse(riverElement.Element("drainage-area").Value);
                }
                if (riverElement.Element("average-discharge") != null)
                {
                    riverEntity.AverageDischarge = int.Parse(riverElement.Element("average-discharge").Value);
                }

                ParseAndAddCountriesToRiver(context, riverElement, riverEntity);

                context.Rivers.Add(riverEntity);
            }

            context.SaveChanges();
        }
예제 #3
0
        static void Main()
        {
            var context = new GeographyEntities();
            // var rivers = context.Rivers.Count(); // check DB

            // Load the XML from file and test print
            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
            //System.Console.WriteLine(xmlDoc);

            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                //extract the mandatory fields
                string riverName    = riverNode.Element("name").Value;
                int    riverLenght  = int.Parse(riverNode.Element("length").Value);
                string riverOutflow = riverNode.Element("outflow").Value;

                //extract the optional fields
                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);
                }

                // Import the parsed rivers into the database
                var river = new River()
                {
                    RiverName        = riverName,
                    Length           = riverLenght,
                    Outflow          = riverOutflow,
                    DrainageArea     = drainageArea,
                    AverageDischarge = averageDischarge
                };


                // Load the countries for each river
                var 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);
                }

                // Save the river in the database
                context.Rivers.Add(river);
                context.SaveChanges();
            }

            System.Console.WriteLine("Rivers imported from rivers.xml");
        }
예제 #4
0
        public static void Main(string[] args)
        {
            var context = new GeographyEntities();

            // Parse the test file rivers.xml
            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");

            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                // mandatory
                string riverName    = riverNode.Element("name").Value;
                int    riverLength  = int.Parse(riverNode.Element("length").Value);
                string riverOutflow = riverNode.Element("outflow").Value;

                // optional
                int?riverDrainageArea = null;
                if (riverNode.Element("drainage-area") != null)
                {
                    riverDrainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                }

                int?riverAverageDischarge = null;
                if (riverNode.Element("average-discharge") != null)
                {
                    riverAverageDischarge = int.Parse(riverNode.Element("average-discharge").Value);
                }

                // Create object for current River
                var river = new Rivers()
                {
                    RiverName        = riverName,
                    Length           = riverLength,
                    DrainageArea     = riverDrainageArea,
                    AverageDischarge = riverAverageDischarge,
                    Outflow          = riverOutflow
                };

                // Add the countries for the rivers
                var 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);
                }

                // Add the rivers to the DB
                context.Rivers.Add(river);
                context.SaveChanges();
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            var xmlDoc     = XDocument.Load(@"..\..\rivers.xml");
            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");
            var context    = new GeographyEntities();

            foreach (var riverNode in riverNodes)
            {
                var    river        = new River();
                string riverName    = riverNode.Element("name").Value;
                int    riverLength  = int.Parse(riverNode.Element("length").Value);
                string riverOutflow = riverNode.Element("outflow").Value;

                int?drainageArea     = null;
                int?averageDischarge = null;

                var riverNodeElement = riverNode.Element("drainage-area");
                if (riverNodeElement != null)
                {
                    drainageArea = int.Parse(riverNodeElement.Value);
                }

                if (riverNode.Element("average-discharge") != null)
                {
                    averageDischarge = int.Parse(riverNode.Element("average-discharge").Value);
                }

                river.RiverName        = riverName;
                river.Length           = riverLength;
                river.Outflow          = riverOutflow;
                river.DrainageArea     = drainageArea;
                river.AverageDischarge = averageDischarge;

                var 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();
            }
        }
예제 #6
0
    static void Main()
    {
        var xmlDoc     = XDocument.Load(@"..\..\rivers.xml");
        var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");
        var context    = new GeographyEntities();

        foreach (var riverNode in riverNodes)
        {
            // Parse the river properties
            var river = new River
            {
                RiverName = riverNode.Descendants("name").First().Value,
                Length    = int.Parse(riverNode.Descendants("length").First().Value),
                Outflow   = riverNode.Descendants("outflow").First().Value
            };
            var drainageArea = riverNode.Descendants("drainage-area").FirstOrDefault();
            if (drainageArea != null)
            {
                river.DrainageArea = int.Parse(drainageArea.Value);
            }
            var averageDischarge = riverNode.Descendants("average-discharge").FirstOrDefault();
            if (averageDischarge != null)
            {
                river.AverageDischarge = int.Parse(averageDischarge.Value);
            }

            // Load the countries for each river
            var countryNodes = riverNode.XPathSelectElements("countries/country");
            foreach (var countryNode in countryNodes)
            {
                var country = context.Countries.
                              FirstOrDefault(c => c.CountryName == countryNode.Value);
                if (country == null)
                {
                    throw new Exception("Can not find country: " + countryNode.Value);
                }
                river.Countries.Add(country);
            }

            // Save the river in the database
            context.Rivers.Add(river);
            context.SaveChanges();
        }

        Console.WriteLine("Rivers imported from rivers.xml");
    }
예제 #7
0
        static void Main(string[] args)
        {
            var context = new GeographyEntities();

            var xmlDoc        = XDocument.Load("../../rivers.xml");
            var riverElements = xmlDoc.Root.Elements();

            foreach (var riverElement in riverElements)
            {
                var riverEntity = new River();

                riverEntity.RiverName = riverElement.Element("name").Value;
                riverEntity.Length    = int.Parse(riverElement.Element("length").Value);
                riverEntity.Outflow   = riverElement.Element("outflow").Value;

                if (riverElement.Element("average-discharge") != null)
                {
                    riverEntity.AverageDischarge = int.Parse(riverElement.Element("average-discharge").Value);
                }

                if (riverElement.Element("drainage-area") != null)
                {
                    riverEntity.DrainageArea = int.Parse(riverElement.Element("drainage-area").Value);
                }

                var countryElements = riverElement.XPathSelectElements("countries/contry");

                foreach (var countryElement in countryElements)
                {
                    var countryName   = countryElement.Value;
                    var countryEntity = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    if (countryEntity != null)
                    {
                        riverEntity.Countries.Add(countryEntity);
                    }
                    else
                    {
                        throw new Exception(String.Format("Cannot find {0} in the DB", countryName));
                    }
                }
                context.Rivers.Add(riverEntity);
            }
            context.SaveChanges();
        }
예제 #8
0
        public static void Main()
        {
            var context = new GeographyEntities();
            //foreach (var continent in context.Continents)
            //{
            //    Console.WriteLine(continent.ContinentName);
            //}

            var rivers = context.Rivers.OrderByDescending(r => r.Length).
                         Select(r =>
                                new
            {
                RiverName = r.RiverName,
                Length    = r.Length,
                Countries = r.Countries.Select(c => c.CountryName)
            });

            //foreach (var river in rivers)
            //{
            //    Console.WriteLine(river.RiverName);
            //    Console.WriteLine(river.Length);
            //    Console.WriteLine(string.Join(", ", river.Countries));
            //}

            //string riversAsJSON = SerializeQueryableToJSON(rivers, "riversJSON.json");
            //Console.WriteLine(riversAsJSON);

            //foreach (var monastery in context.Monasteries)
            //{
            //    Console.WriteLine(monastery.Name);
            //}

            var countriesWithMonasteries = context.Countries.OrderBy(c => c.CountryName).
                                           Where(c => c.Monasteries.Any()).
                                           Select(c =>
                                                  new
            {
                CountryName = c.CountryName,
                Monasteries = c.Monasteries.OrderBy(m => m.Name).
                              Select(m => m.Name)
            });

            //foreach (var country in countriesWithMonasteries)
            //{
            //    Console.WriteLine(country.CountryName + " " +
            //        string.Join(", ", country.Monasteries));
            //}

            //var countriesWithMonasteriesXMLRootElement = new XElement("countries-with-monasteries");

            //foreach (var country in countriesWithMonasteries)
            //{
            //    var countryXML = new XElement("country");
            //    countryXML.Add(new XAttribute("name", country.CountryName));
            //    countriesWithMonasteriesXMLRootElement.Add(countryXML);

            //    foreach (var monastery in country.Monasteries)
            //    {
            //        countryXML.Add(new XElement("monastery", monastery));
            //    }
            //}

            //var countriesWithMonasteriesXML = new XDocument(countriesWithMonasteriesXMLRootElement);
            //countriesWithMonasteriesXML.Save("countriesWithMonasteriesXML.xml");

            //Console.WriteLine(countriesWithMonasteriesXML);

            var riversXML = XDocument.Load("../../rivers.xml");
            //Console.WriteLine(riversXML);

            var riverNodes = riversXML.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                string riverName    = riverNode.Element("name").Value;
                int    riverLength  = 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?discharge = null;
                if (riverNode.Element("average-discharge") != null)
                {
                    discharge = int.Parse(riverNode.Element("average-discharge").Value);
                }

                var river = new River()
                {
                    RiverName        = riverName,
                    Length           = riverLength,
                    Outflow          = riverOutflow,
                    DrainageArea     = drainageArea,
                    AverageDischarge = discharge
                };

                context.Rivers.Add(river);

                var 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.SaveChanges();
        }
예제 #9
0
        static void Main()
        {
            var context = new GeographyEntities();
            var rivers  = context.Rivers
                          .Select(r => new
            {
                r.RiverName,
                r.Length,
                r.Outflow,
                r.DrainageArea,
                r.AverageDischarge,
                Countries = r.Countries
                            .Select(c => c.CountryName)
                            .ToList()
            })
                          .ToList();

            var xmlDoc = XDocument.Load(@"..\..\riversToAdd.xml");

            var riversNodes = (xmlDoc.XPathSelectElements("rivers/river")).ToList();

            riversNodes.ForEach(rn =>
            {
                var countryNodes = rn.XPathSelectElements("countries/country");
                var countryNames = countryNodes.Select(c => c.Value).ToList();

                string riverName      = rn.Element("name").Value;
                int riverLength       = int.Parse(rn.Element("length").Value);
                string riverOutFlow   = rn.Element("outflow").Value;
                int?riverDrainage     = null;
                int?riverAvgDischarge = null;

                if (rn.Element("drainage-area") != null)
                {
                    riverDrainage = int.Parse(rn.Element("drainage-area").Value);
                }

                if (rn.Element("average-discharge") != null)
                {
                    riverAvgDischarge = int.Parse(rn.Element("average-discharge").Value);
                }

                var dbRiver = context.Rivers.FirstOrDefault(r => r.RiverName.Equals(riverName));
                countryNames.ForEach(cn =>
                {
                    if (!dbRiver.Countries.Contains(context.Countries.First(c => c.CountryName.Equals(cn))))
                    {
                        dbRiver.Countries.Add(context.Countries.First(c => c.CountryName.Equals(cn)));
                    }
                });

                //countryNames.ForEach(cn =>
                //{
                //    var country = context.Countries
                //        .FirstOrDefault(c => c.CountryName == cn);
                //    river.Countries.Add(country);
                //});
                context.Rivers.AddOrUpdate(dbRiver);
                context.SaveChanges();


                //Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}",
                //    riverName,
                //    riverLength,
                //    riverOutFlow,
                //    riverDrainage,
                //    riverAvgDischarge);

                //Console.WriteLine("{0} -> {1}", riverName, string.Join(", ", countries));
            });

            //rivers.ForEach(r =>
            //{
            //    Console.WriteLine("{0}, {1}, {2}, \"{3}\", \"{4}\"",
            //        r.RiverName,
            //        r.Length,
            //        r.Outflow,
            //        r.DrainageArea,
            //        r.AverageDischarge);
            //    r.Countries.ForEach(c =>
            //    {
            //        Console.WriteLine("{0}",c);
            //    });
            //    Console.WriteLine();
            //});

            //var xmlRivers = new XElement("rivers");

            //    rivers.ForEach(r =>
            //    {
            //        var xmlRiver = new XElement("river");
            //        xmlRivers.Add(xmlRiver);
            //        xmlRiver.Add(new XElement("name", r.RiverName));
            //        xmlRiver.Add(new XElement("length", r.Length));
            //        xmlRiver.Add(new XElement("outflow", r.Outflow));
            //        var drainageArea = r.DrainageArea;
            //        var averageDischarge = r.AverageDischarge;
            //        var countries = r.Countries;

            //        if (drainageArea != null)
            //        {
            //            xmlRiver.Add(new XElement("drainage-area", drainageArea));
            //        }
            //        if (averageDischarge != null)
            //        {
            //            xmlRiver.Add(new XElement("average-discharge", averageDischarge));
            //        }
            //        if (countries.Count > 0)
            //        {
            //            var xmlCountries = new XElement("countries");
            //            countries.ForEach(c =>
            //            {
            //                var xmlCountry = new XElement("contry", c);
            //                xmlCountries.Add(xmlCountry);
            //            });
            //            xmlRiver.Add(xmlCountries);
            //        }
            //    });

            //var xmlDoc = new XDocument(xmlRivers);
            //xmlDoc.Save("rivers.xml");
            //Console.WriteLine(xmlDoc);
        }
예제 #10
0
 private static void ImportRiver(GeographyEntities context, River river)
 {
     context.Rivers.AddOrUpdate(river);
     context.SaveChanges();
     Console.WriteLine("{0} saved to the db.", river.RiverName);
 }
예제 #11
0
 private static void ImportRiver(GeographyEntities context, River river)
 {
     context.Rivers.AddOrUpdate(river);
     context.SaveChanges();
     Console.WriteLine("{0} saved to the db.", river.RiverName);
 }