/// <summary> /// When program startup, the program will read default data file /// Default data file should be store in Debug or Release Folder /// If the defualt data file is not found, we should use Load Data feature /// to load data file into our program /// </summary> private void LoadDefaultDataFile() { string fileName = ConfigurationManager.AppSettings["XMLDataFile"]; string path = PathHelper.GetFileLocation(fileName); if (File.Exists(path)) { try { XMLHelpers.ReadFromXML(fileName); } catch { // } } }
/// <summary> /// Open a dialog to select the xml file /// Load data from xml file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnLoadData_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); openFileDialog.DefaultExt = ".xml"; openFileDialog.Filter = "XML file (*.xml)|*.xml"; if (openFileDialog.ShowDialog() == true) { // a flag for store status of reading xml file, and catch exception message to display for users. try { XMLHelpers.ReadFromXML(openFileDialog.FileName); scheduleViewModel.AppointmentList = XMLHelpers.Instance().Appointments; scheduleViewModel.RefreshAppointments(); } catch (Exception error) { MessageBox.Show(error.Message, "Failed to read " + System.IO.Path.GetFileName(openFileDialog.FileName), MessageBoxButton.OK, MessageBoxImage.Error); return; } } }