Beispiel #1
0
        private void btnXmlDeserialize_Click(object sender, RoutedEventArgs e)
        {
            Stream         stream      = null;
            OpenFileDialog dlgOpenFile = new OpenFileDialog();
            XMLTransformer serializer  = new XMLTransformer(typeof(List <Employee>));

            dlgOpenFile.Filter = "xml files (*.xml)|*.xml";

            if (dlgOpenFile.ShowDialog() == true)
            {
                try
                {
                    if ((stream = dlgOpenFile.OpenFile()) != null)
                    {
                        using (stream)
                        {
                            Employees.Clear();
                            Employees = (List <Employee>)serializer.ReadObject(serializer.DecodeXml(stream, _plugins));
                        }
                    }
                }
                catch (IOException)
                {
                    MessageBox.Show("Cannot deserialize data.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (Exception) { }
            }

            UpdateProjectsList();
            RefreshDataGrids();
        }
Beispiel #2
0
        private void btnXmlSerialize_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlgSaveFile = new SaveFileDialog();

            Type tEmployee = typeof(Employee);

            Type[] jobs = Assembly.GetAssembly(tEmployee).GetTypes()
                          .Where(type => (type.IsSubclassOf(tEmployee) && !type.IsAbstract)).ToArray();

            XMLTransformer serializer = new XMLTransformer(typeof(List <Employee>), jobs);

            dlgSaveFile.Filter = "xml files (*.xml)|*.xml";

            if (dlgSaveFile.ShowDialog() == true && dlgSaveFile.FileName != "")
            {
                try
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        serializer.WriteObject(stream, Employees);
                        XmlDocument xmlDoc = serializer.EncodeXml(stream, (string)cbPlugins.SelectedItem, _plugins);
                        using (XmlTextWriter writer = new XmlTextWriter(dlgSaveFile.FileName, null))
                        {
                            xmlDoc.Save(writer);
                        }
                    }
                }
                catch (IOException)
                {
                    MessageBox.Show("Cannot serialize data.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }