コード例 #1
0
        private void Button_Open_Click(object sender, RoutedEventArgs e)
        {
            string filePath = TextBox_FilePath.Text;    //TODO add check if filePath is valid

            MainConfig.CurrentEntity = ComboBox_EntityType.SelectedItem.ToString();

            #region ParseFileFormat
            Regex  regEx  = new Regex(@"(?<=.+)\..+");  // *.fileFormat
            string format = regEx.Match(TextBox_FilePath.Text).Value;
            #endregion

            switch (format)
            {
            case ".xml":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    XmlSerializationService <List <Product> > xmlService = new XmlSerializationService <List <Product> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _products = xmlService.Read(filePath, MainConfig.FileReadMode);
                    }
                    DataGrid_Main.ItemsSource = _products;
                }
                break;

                case "Student":
                {
                    XmlSerializationService <List <Student> > xmlService = new XmlSerializationService <List <Student> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _students = xmlService.Read(filePath, MainConfig.FileReadMode);
                    }
                    DataGrid_Main.ItemsSource = _students;
                }
                break;
                }
            }
            break;

            case ".json":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    JsonSerializationService <List <Product> > jsonService = new JsonSerializationService <List <Product> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _products = jsonService.Read(filePath, MainConfig.FileReadMode);
                    }
                    DataGrid_Main.ItemsSource = _products;
                }
                break;

                case "Student":
                {
                    JsonSerializationService <List <Student> > jsonService = new JsonSerializationService <List <Student> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _students = jsonService.Read(filePath, MainConfig.FileReadMode);
                    }
                    DataGrid_Main.ItemsSource = _students;
                }
                break;
                }
            }
            break;

            case ".bin":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    BinarySerializationService <List <Product> > binService = new BinarySerializationService <List <Product> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _products = binService.Read(filePath, MainConfig.FileReadMode);
                    }
                    DataGrid_Main.ItemsSource = _products;
                }
                break;

                case "Student":
                {
                    BinarySerializationService <List <Student> > binService = new BinarySerializationService <List <Student> >();
                    if (new FileInfo(filePath).Length != 0)
                    {
                        _students = binService.Read(filePath, MainConfig.FileReadMode);
                    }
                }
                break;
                }
            }
            break;
                //TODO add default with exception
            }

            Button_Open.IsEnabled = false;
        }
コード例 #2
0
        private void Button_Save_Click(object sender, RoutedEventArgs e)
        {
            string filePath = TextBox_FilePath.Text;

            #region ParseFileFormat
            Regex  regEx  = new Regex(@"(?<=.+)\..+");  // *.fileFormat
            string format = regEx.Match(TextBox_FilePath.Text).Value;
            #endregion

            switch (format)
            {
            case ".xml":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    XmlSerializationService <List <Product> > xmlService = new XmlSerializationService <List <Product> >();
                    xmlService.Write((List <Product>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;

                case "Student":
                {
                    XmlSerializationService <List <Student> > xmlService = new XmlSerializationService <List <Student> >();
                    xmlService.Write((List <Student>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;
                }
            }
            break;

            case ".json":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    JsonSerializationService <List <Product> > jsonService = new JsonSerializationService <List <Product> >();
                    jsonService.Write((List <Product>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;

                case "Student":
                {
                    JsonSerializationService <List <Student> > jsonService = new JsonSerializationService <List <Student> >();
                    jsonService.Write((List <Student>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;
                }
            }
            break;

            case ".bin":
            {
                switch (ComboBox_EntityType.SelectedItem.ToString())
                {
                case "Product":
                {
                    BinarySerializationService <List <Product> > binService = new BinarySerializationService <List <Product> >();
                    binService.Write((List <Product>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;

                case "Student":
                {
                    BinarySerializationService <List <Student> > binService = new BinarySerializationService <List <Student> >();
                    binService.Write((List <Student>)DataGrid_Main.ItemsSource, filePath, MainConfig.FileWriteMode);
                }
                break;
                }
            }
            break;
                //TODO add default with exception
            }
        }