Пример #1
0
        private void btn_SaveToXML_Click(object sender, RoutedEventArgs e)
        {
            string        fileName      = "";
            SerialezerXML serialezerXML = new SerialezerXML();

            DeletePersons();
            InitPersons();
            Phone          phone = new Phone();
            SaveFileDialog dlg   = new SaveFileDialog();

            dlg.Filter = "XML Files *.xml|*.xml";
            if (dlg.ShowDialog() == true)
            {
                fileName = dlg.FileName;
            }

            serialezerXML.persons = mPersons;
            serialezerXML.phones  = myPhonesList;
            XmlSerializer serializer = new XmlSerializer(typeof(SerialezerXML), "Serialization");

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serialezerXML);

                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    stream.WriteTo(fs);
                    fs.Flush();
                }
            }
        }
Пример #2
0
        private void btn_LoadXML_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "";
            // SensorsDataGrid.ItemsSource = phoneCollect;
            // SensorsDataGrid.Items.CurrentChanging += CellChanging;
            SerialezerXML serialezerXML = new SerialezerXML();

            OpenFileDialog dlg = new OpenFileDialog();

            mPersons.Clear();
            myPhonesList.Clear();
            phoneCollect.Clear();

            dlg.Filter = "XML Files *.xml|*.xml";
            if (dlg.ShowDialog() == true)
            {
                fileName = dlg.FileName;
            }
            XmlSerializer serializer = new XmlSerializer(typeof(SerialezerXML), "Serialization");

            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                serialezerXML = (SerialezerXML)serializer.Deserialize(fs);
                fs.Close();
            }

            mPersons      = serialezerXML.persons;
            myPhonesList  = serialezerXML.phones;
            Phone.idPhone = 0;

            for (int i = 0; i < myPhonesList.Count; i++)
            {
                Phone phone = new Phone();
                phone = myPhonesList[i];
                AddPhoneCollect(phone);
                Phone.AddId();
                phoneCollect[Phone.idPhone - 1].idNumber = Phone.idPhone;
            }
        }