コード例 #1
0
        static void Main()
        {
            using (var context = new GeographyEntities())
            {
                var xmlDoc = XDocument.Load("../../rivers.xml");

                //Console.WriteLine(xmlDoc);

                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("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(riverElement, context, riverEntity);

                    context.Rivers.Add(riverEntity);
                }

                context.SaveChanges();
            }
        }
        static void Main()
        {
            var context = new GeographyEntities();

            XDocument xmlDoc = XDocument.Load("../../rivers.xml");

            var riversByXml =
                from river in xmlDoc.Descendants("river")
                select new
                {
                    RiverName = river.Element("name").Value,
                    Length = river.Element("length").Value,
                    Outflow = river.Element("outflow").Value,
                    DrainageArea = river.Element("drainage-area") != null ? river.Element("drainage-area").Value : null,
                    AverageDischarge = river.Element("average-discharge") != null ? river.Element("average-discharge").Value : null,
                    Countries = river.Element("countries") != null ? river.Element("countries").Elements() : null
                };

            foreach (var river in riversByXml)
            {
                var riverDb = new River();

                riverDb.RiverName = river.RiverName;
                riverDb.Length = int.Parse(river.Length);
                riverDb.Outflow = river.Outflow;

                if(!string.IsNullOrEmpty(river.DrainageArea))
                {
                    riverDb.DrainageArea = int.Parse(river.DrainageArea);
                }

                if (!string.IsNullOrEmpty(river.AverageDischarge))
                {
                    riverDb.AverageDischarge = int.Parse(river.AverageDischarge);
                }

                if (river.Countries != null)
                {
                    foreach (var country in river.Countries)
                    {
                        var countryDb = context.Countries
                            .Where(c => c.CountryName == country.Value)
                            .FirstOrDefault();

                        riverDb.Countries.Add(countryDb);
                    }
                }

                context.Rivers.Add(riverDb);
            }

            context.SaveChanges();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var context = new GeographyEntities();
            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);
                }

                var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countries = countryNodes.Select(c => c.Value);

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

                context.Rivers.Add(river);

                foreach (var countryName in countries)
                {
                    var country = context.Countries
                        .FirstOrDefault(c => c.CountryName == countryName);

                    river.Countries.Add(country);
                }

                context.SaveChanges();
            }
        }
コード例 #4
0
 private static void ParseAndAddCountriesToRiver(
     XElement riverElement, GeographyEntities context, River riverEntity)
 {
     var countryElements = riverElement.XPathSelectElements("countries/country");
     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 country {0} in the DB", countryName));
         }
     }
 }
コード例 #5
0
        public static void Main()
        {
            var context = new GeographyEntities();

            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                //Console.WriteLine(riverNode);
                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);
                }

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

                context.Rivers.Add(river);
                context.SaveChanges();

                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();
                //List<string> countries = new List<string>();
                //if (riverNode.Element("countries") != null)
                //{
                //var riverCountries = riverNode.Descendants("country");
                //int counter = 0;

                //foreach (var country in riverCountries)
                //{
                //    countries.Add(country.Value);
                //}

                //Second Way
                //}
            }
        }
コード例 #6
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");
        }