private Workout CreatePopulatedWorkout(XDocument nikePlusDoc) { ValidateStatus(nikePlusDoc); Workout work = new Workout{ImportedOn = DateTime.Now}; var baseElement = nikePlusDoc.Root.Element("sportsData"); if(baseElement != null){ work.EventDate = (DateTime)baseElement.Element("startTime"); work.Comments = baseElement.Element("description").Value; work.Name = baseElement.Element("name").Value; var run = baseElement.Element("runSummary"); if(run != null){ work.Calories = (float)run.Element("calories"); work.Duration = (int)run.Element("duration"); work.Distance = (float)run.Element("distance"); var heartrate = run.Element("heartrate"); if (heartrate != null) { work.HeartRateAvg = (short)heartrate.Element("average"); work.HeartRateMax = (short)heartrate.Element("maximum").Element("bpm"); work.HeartRateMin = (short)heartrate.Element("minimum").Element("bpm"); } } var extendedData = baseElement.Element("extendedDataList"); if (extendedData != null) { work.Snapshots = from n in extendedData.Elements("extendedData") select new Workout.SnapShot { DataType = n.Attribute("dataType").Value, Interval = (int) n.Attribute("intervalValue"), IntervalType = n.Attribute("intervalType").Value, IntervalUnit = n.Attribute("intervalUnit").Value, Intervals = n.Value.Split(',').Select(p => Convert.ToSingle(p.Trim())) }; } } return work; }
private IEnumerable<Workout> CreateSimpleWorkouts(XDocument nikePlusDoc) { ValidateStatus(nikePlusDoc); IList<Workout> workouts = new List<Workout>(); foreach (var run in nikePlusDoc.Descendants("run")) { var workout = new Workout{ Calories= (float) run.Element("calories"), Duration= (int)run.Element("duration"), Distance= (float) run.Element("distance"), EventDate = (DateTime) run.Element("startTime"), ID = (long) run.Attribute("id"), ImportedOn = DateTime.Now, Comments= run.Element("description").Value, Name = run.Element("name").Value}; var heartrate = run.Element("heartrate"); if(heartrate != null) { workout.HeartRateAvg = (short) heartrate.Element("average"); workout.HeartRateMax = (short) heartrate.Element("maximum"); workout.HeartRateMin = (short) heartrate.Element("minimum"); } workouts.Add(workout); } return workouts; }