示例#1
0
        private void ButParseXmlClick(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog {
                InitialDirectory = "D:\\downloads",                           //Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}", string.Empty).ToString()
                Filter           = "XML-Files|*.xml"
            };

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var ser = new XmlSerializer(typeof(polarexercisedata));

            calendaritem[] foo;
            using (var fs = new FileStream(dlg.FileName, FileMode.Open))
                foo = ((polarexercisedata)ser.Deserialize(fs)).calendaritems.Items;
            var exercises    = new List <exercisedata>();
            var invalidCount = 0;

            foreach (var f in foo)
            {
                if (f is exercisedata)
                {
                    exercises.Add((exercisedata)f);
                }
                else
                {
                    invalidCount++;
                }
            }

            if (exercises.Count == 1 && invalidCount > 0)
            {
                MessageBox.Show("Ignoring " + invalidCount + " invalid entries", "Invalid data",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            var res = DialogResult.Yes;

            if (exercises.Count > 1)
            {
                res = MessageBox.Show(exercises.Count + " exercises found" + (invalidCount > 0 ? " as well as " + invalidCount + " other entries which will be skipped. " : ".") + " Entry forms for each exercise will be opened sequentially. Do you want to continue?", exercises.Count + " exercises found", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }

            if (res == DialogResult.No)
            {
                return;
            }

            Action             openTef        = null;
            CancelEventHandler preventClosing = (ss, ee) =>
            {
                ee.Cancel = true;
                openTef();
            };

            openTef = () =>
            {
                var durationString = exercises[0].result.duration.Contains(".")
                    ? exercises[0].result.duration.Remove(exercises[0].result.duration.IndexOf('.'))
                    : exercises[0].result.duration;
                if (durationString.EndsWith(":"))
                {
                    durationString += "00";
                }
                FillEntryData(new TrainingEntry
                {
                    AverageHr = exercises[0].result.heartrate.average,
                    Calories  = int.Parse(exercises[0].result.calories),
                    Date      = DateTime.Parse(exercises[0].time),
                    DistanceM = (int)exercises[0].result.distance,
                    Duration  = TimeSpan.Parse(durationString),
                    HrZones   =
                        ZoneData.Parse(exercises[0].result.zones[4].inzone + "_" +
                                       exercises[0].result.zones[3].inzone +
                                       "_" + exercises[0].result.zones[2].inzone +
                                       "_" +
                                       exercises[0].result.zones[1].inzone + "_" +
                                       exercises[0].result.zones[0].inzone),
                    Note  = exercises[0].note,
                    Sport = exercises[0].sport == "Other sport" ? Common.Sport.Other :
                            (Common.Sport)Enum.Parse(typeof(Common.Sport), exercises[0].sport)
                });
                exercises.RemoveAt(0);

                if (exercises.Count == 0)
                {
                    Closing -= preventClosing;
                }
            };

            if (exercises.Count > 1)
            {
                Closing += preventClosing;
            }

            openTef();



            //    if (exercises.Count == 1)
            //    {
            //        txtDuration.Text = exercises[0].result.duration.Remove(exercises[0].result.duration.IndexOf('.'));
            //        txtAvgHR.Text = exercises[0].result.heartrate.average.ToString(CultureInfo.InvariantCulture);
            //        txtCalories.Text = exercises[0].result.calories;
            //        txtDistance.Text = (exercises[0].result.distance / 1000).ToString(CultureInfo.InvariantCulture);
            //        datDate.Value = DateTime.Parse(exercises[0].time);
            //        comSport.Text = exercises[0].sport.Equals("Other sport") ? "Other" : exercises[0].sport;
            //        txtNotes.Text = exercises[0].note;
            //        txtZone5.Text = exercises[0].result.zones[4].inzone;
            //        txtZone4.Text = exercises[0].result.zones[3].inzone;
            //        txtZone3.Text = exercises[0].result.zones[2].inzone;
            //        txtZone2.Text = exercises[0].result.zones[1].inzone;
            //        txtZone1.Text = exercises[0].result.zones[0].inzone;
            //        Show();
            //    }
            //    else
            //    {
            //        var tef = new TrainingEntryForm(new TrainingEntry
            //            {
            //                AverageHr = exercises[0].result.heartrate.average,
            //                Calories = int.Parse(exercises[0].result.calories),
            //                Date = DateTime.Parse(exercises[0].time),
            //                DistanceM = (int)exercises[0].result.distance,
            //                Duration = TimeSpan.Parse(exercises[0].result.duration.Remove(exercises[0].result.duration.IndexOf('.'))),
            //                HrZones =
            //                    ZoneData.Parse(exercises[0].result.zones[4].inzone + "_" +
            //                                    exercises[0].result.zones[3].inzone +
            //                                    "_" + exercises[0].result.zones[2].inzone +
            //                                    "_" +
            //                                    exercises[0].result.zones[1].inzone + "_" +
            //                                    exercises[0].result.zones[0].inzone),
            //                Note = exercises[0].note,
            //                Sport =
            //                    (Common.Sport)
            //                    Enum.Parse(typeof(Common.Sport), exercises[0].sport)
            //            });

            //        tef.Closing += (ss, ee) => openTef();

            //        tef.Show();
            //    }
            //    exercises.RemoveAt(0);
            //};

            //openTef();

            //    var te = new TrainingEntry
            //{
            //    AverageHr = bar.result.heartrate.average,
            //    Calories = int.Parse(bar.result.calories),
            //    Date = DateTime.Parse(bar.time),
            //    DistanceM = (int)bar.result.distance,
            //    Duration = TimeSpan.Parse(bar.result.duration),
            //    HrZones =
            //        ZoneData.Parse(bar.result.zones[4].inzone + "_" + bar.result.zones[3].inzone +
            //                       "_" + bar.result.zones[2].inzone + "_" +
            //                       bar.result.zones[1].inzone + "_" + bar.result.zones[0].inzone),
            //    Note = bar.note,
            //    Sport = (Common.Sport)Enum.Parse(typeof(Common.Sport), bar.sport)
            //};
        }