protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { XDocument xdoc = XDocument.Load("days.xml"); XElement root = xdoc.Root; //Database this.workoutDB = new WorkoutContext(WorkoutContext.DBConnectionString); // Gather all exercises only // For each day foreach (XElement element in root.Nodes()) { int num = Convert.ToInt32((element.Descendants("num").Single() as XElement).Value); Day day = new Day(); day.Workout = (element.Descendants("workout").Single() as XElement).Value.ToString(); day.Num = num; day.VideoKey = ((element.Descendants("video").Single() as XElement).Descendants("video_key").Single() as XElement).Value; day.ThumbnailUrl = ((element.Descendants("video").Single() as XElement).Descendants("thumbnail_url").Single() as XElement).Value; this.workoutDB.Days.InsertOnSubmit(day); this.workoutDB.SubmitChanges(); if (element.Elements().Count() == 4) { XElement exerciseRoot = element.Descendants("exercises").Single() as XElement; // For each exercise foreach (XElement exec in exerciseRoot.Nodes()) { Exercise exercise = new Exercise(); exercise.Name = (exec.Descendants("name").Single() as XElement).Value.ToString(); try { this.workoutDB.Exercises.First(ex => ex.Name == exercise.Name); } catch (Exception ex) { this.workoutDB.Exercises.InsertOnSubmit(exercise); Console.WriteLine(ex); } this.workoutDB.SubmitChanges(); } } } List<Exercise> exercisesInDB = (from Exercise exercise in workoutDB.Exercises select exercise).ToList(); List<Day> daysInDB = (from Day day in workoutDB.Days select day).ToList(); // Days foreach (XElement element in root.Nodes()) { int num = Convert.ToInt32((element.Descendants("num").Single() as XElement).Value); // Exercises are also present if (element.Elements().Count() == 4) { XElement exerciseRoot = element.Descendants("exercises").Single() as XElement; foreach (XElement exec in exerciseRoot.Nodes()) { DayExercise dayExercise = new DayExercise(); string exerciseName = (exec.Descendants("name").Single() as XElement).Value.ToString(); string type = (exec.Descendants("type").Single() as XElement).Value.ToString(); Exercise exercise = exercisesInDB.Single(ex => ex.Name == exerciseName); Day day = daysInDB.Single(d => d.Num == num); dayExercise.ExerciseId = exercise.ExerciseId; dayExercise.DayId = day.DayId; dayExercise.ExerciseSetType = decodeSetType(type); dayExercise.Description = (exec.Descendants("description").Single() as XElement).Value.ToString().Trim(new char[] { '\n', ' ' }); ; Regex rgx = new Regex("\n\\s*"); dayExercise.Description = rgx.Replace(dayExercise.Description, "\n"); this.workoutDB.DayExercises.InsertOnSubmit(dayExercise); } } } this.workoutDB.SubmitChanges(); // Call the base method. base.OnNavigatedTo(e); }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { XDocument xdoc = XDocument.Load("days.xml"); XElement root = xdoc.Root; //Database this.workoutDB = new WorkoutContext(WorkoutContext.DBConnectionString); // Gather all exercises only // For each day foreach (XElement element in root.Nodes()) { int num = Convert.ToInt32((element.Descendants("num").Single() as XElement).Value); Day day = new Day(); day.Workout = (element.Descendants("workout").Single() as XElement).Value.ToString(); day.Num = num; day.VideoKey = ((element.Descendants("video").Single() as XElement).Descendants("video_key").Single() as XElement).Value; day.ThumbnailUrl = ((element.Descendants("video").Single() as XElement).Descendants("thumbnail_url").Single() as XElement).Value; this.workoutDB.Days.InsertOnSubmit(day); this.workoutDB.SubmitChanges(); if (element.Elements().Count() == 4) { XElement exerciseRoot = element.Descendants("exercises").Single() as XElement; // For each exercise foreach (XElement exec in exerciseRoot.Nodes()) { Exercise exercise = new Exercise(); exercise.Name = (exec.Descendants("name").Single() as XElement).Value.ToString(); try { this.workoutDB.Exercises.First(ex => ex.Name == exercise.Name); } catch (Exception ex) { this.workoutDB.Exercises.InsertOnSubmit(exercise); Console.WriteLine(ex); } this.workoutDB.SubmitChanges(); } } } List <Exercise> exercisesInDB = (from Exercise exercise in workoutDB.Exercises select exercise).ToList(); List <Day> daysInDB = (from Day day in workoutDB.Days select day).ToList(); // Days foreach (XElement element in root.Nodes()) { int num = Convert.ToInt32((element.Descendants("num").Single() as XElement).Value); // Exercises are also present if (element.Elements().Count() == 4) { XElement exerciseRoot = element.Descendants("exercises").Single() as XElement; foreach (XElement exec in exerciseRoot.Nodes()) { DayExercise dayExercise = new DayExercise(); string exerciseName = (exec.Descendants("name").Single() as XElement).Value.ToString(); string type = (exec.Descendants("type").Single() as XElement).Value.ToString(); Exercise exercise = exercisesInDB.Single(ex => ex.Name == exerciseName); Day day = daysInDB.Single(d => d.Num == num); dayExercise.ExerciseId = exercise.ExerciseId; dayExercise.DayId = day.DayId; dayExercise.ExerciseSetType = decodeSetType(type); dayExercise.Description = (exec.Descendants("description").Single() as XElement).Value.ToString().Trim(new char[] { '\n', ' ' });; Regex rgx = new Regex("\n\\s*"); dayExercise.Description = rgx.Replace(dayExercise.Description, "\n"); this.workoutDB.DayExercises.InsertOnSubmit(dayExercise); } } } this.workoutDB.SubmitChanges(); // Call the base method. base.OnNavigatedTo(e); }