コード例 #1
0
        /// <summary>
        /// Use XmlColumn to store the Xml file in the database
        /// </summary>
        /// <param name="document">the Xml file we need to store in the datase</param>
        static void ImportYearCourses(XDocument document)
        {
            using (MySchoolContext school = new MySchoolContext())
            {
                // Set the value of Courses property with the Xml document to store the Xml file.
                YearCourse yearCourse = new YearCourse {
                    Year = 2012, Courses = document
                };

                school.YearCourses.Add(yearCourse);

                school.SaveChanges();
            }
        }
コード例 #2
0
        /// <summary>
        /// Use LinqToXml to import information in the xml file into the database
        /// </summary>
        /// <param name="document">the Xml file that we import into the database</param>
        static void ImportCourses(XDocument document)
        {
            using (MySchoolContext school = new MySchoolContext())
            {
                // Get the Course information from the Xml document
                IEnumerable <Course> courses =
                    from c in document.Descendants("Course")
                    select new Course
                {
                    CourseID = c.Element("CourseId") == null?
                               Guid.NewGuid().ToString() : c.Element("CourseId").Value,
                                   Title      = c.Element("Title") == null ? null : c.Element("Title").Value,
                                   Credits    = c.Element("Credits") == null ? -1 : Int32.Parse(c.Element("Credits").Value),
                                   Department = c.Element("Department") == null ? null : c.Element("Department").Value
                };

                foreach (Course course in courses)
                {
                    school.Courses.Add(course);
                }

                school.SaveChanges();
            }
        }