关于将类实现为xml序列化的困惑

时间:2011-02-23 06:39:20

标签: c# wpf xmlserializer

我正在读一本关于序列化的书。我对实现类部分感到困惑,或者只是把我真的不理解类(也许)。

基本上我按照本书的步骤以这种方式设计了我的应用程序。我只是不了解FileSaving的类,也许不知道在哪里实现它。我想开始使用序列化来保存表单的元素,并使用“保存”和“加载”按钮通过xml重新加载它。我有一个文本框和用户键到Majorversiontextbox的字符串,然后这个textbox.text存储为MajorversionLabel。请澄清我的疑虑并帮助我进行序列化。谢谢!我将澄清对我的问题的任何怀疑。

修改

问题1,当我只从标签中的内容获取值并将其保存到xml文件中时,为什么需要使用此FileSaving类?问题2,我的FileSaving类是否正确声明?问题3,为什么我需要获取并设置在这里?

    public partial class Window1 : Window
{
...
...

        public class FileSaving
        {
            private string major;

            public string Majorversion
            {   
                get
                {   
                return major;
                }
                set
                {
                    major = value;
                }
            }
        }
        private void MajorversionupdateButton_Click(object sender, RoutedEventArgs e)
        {
        MajorversionresultLabel.Content = MajorversionTextBox.Text;
        MajorversionupdateButton.Visibility = Visibility.Hidden;
        MajorversionTextBox.Visibility = Visibility.Hidden;
        MajorversionmodifyButton.Visibility = Visibility.Visible;

        }
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string savepath;
            SaveFileDialog DialogSave = new SaveFileDialog();
            // Default file extension
            DialogSave.DefaultExt = "txt";
            // Available file extensions
            DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
            // Adds a extension if the user does not
            DialogSave.AddExtension = true;
            // Restores the selected directory, next time
            DialogSave.RestoreDirectory = true;
            // Dialog title
            DialogSave.Title = "Where do you want to save the file?";
            // Startup directory
            DialogSave.InitialDirectory = @"C:/";
            DialogSave.ShowDialog();
            savepath = DialogSave.FileName;
            DialogSave.Dispose();
            DialogSave = null;

            Filesaving abc = new FileSaving();
            abc.Majorversion = MajorversionLabel.Content;
            FileStream savestream = new FileStream(path, FileMode.Create);
            XmlSerializer serializer = new XmlSerializer(typeof(FileSaving));
            serializer.Serialize(savestream, abc);
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {

            string loadpath;
            Stream checkStream = null;
            Microsoft.Win32.OpenFileDialog DialogLoad = new Microsoft.Win32.OpenFileDialog();
            DialogLoad.Multiselect = false;
            DialogLoad.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
            if ((bool)DialogLoad.ShowDialog())
            {
                try
                {
                    if ((checkStream = DialogLoad.OpenFile()) != null)
                    {
                        loadpath = DialogLoad.FileName;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Problem occured, try again later");
            }

            FileSaving abc = new FileSaving();
            FileStream loadstream = new FileStream(loadpath, FileMode.Open);
            XmlSerializer serializer = new XmlSerializer(typeof(FileSaving));
            abc=(FileSaving)serializer.Deserialize(loadstream);
            loadstream.Close();
            MajorversionresultLabel.Content = abc.Majorversion;
        }

}

1 个答案:

答案 0 :(得分:0)

  1. 因为XmlSerializer是对象序列化程序,并且从类型定义
  2. 开始工作
  3. 当然,那就行了
  4. 严格来说你没有,但目前的版本看起来不错; XmlSerializer适用于公共财产或领域;您可以简化为自动实现的属性:public string MajorVersion {get;set;}
  5. 但是如果你只想保存一个字符串值 - 是xml overfkill吗? File.WriteAllText / File.ReadAllText可能更简单。