对将类实现到 xml 序列化感到困惑

发布于 2024-10-19 07:07:41 字数 3773 浏览 1 评论 0原文

我正在读一本关于连载的书。我对实现类部分感到困惑,或者简单地说我并不真正理解类(也许)。

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

编辑

问题 1,当我只从标签的内容中获取值并将其保存到 xml 文件中时,为什么我需要这个 FileSaving 类。问题2,我的FileSaving类声明正确吗?问题3,为什么这里需要Get和Set?

    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;
        }

}

I was reading a book on serialization. I am confused about implementing class portion or simply put i don't really understand classes(maybe).

Basically i have my application designed in this way following the steps of my book. I just dont understand the class of FileSaving and maybe where to implement it. I would like to start using serialization to save elements of a form and reloading it via xml using a "Save" and "Load" button. I have a textbox and users keys in strings into the Majorversiontextbox and this textbox.text is then stored as MajorversionLabel. Please clarify my doubts and help me out with serialization. Thanks! I will clarify any doubts about my question.

EDIT

Question 1, Why do i need to have this FileSaving class when I am only getting values from the content from the label and saving it into a xml file. Question 2, is my FileSaving class declared correctly? Question 3, why do i need Get and Set over here?

    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;
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-10-26 07:07:42
  1. 因为 XmlSerializer 是一个对象序列化器,并且可以从类型定义中工作
  2. ,严格来说,它不会工作
  3. ,但当前版本看起来不错; XmlSerializer 适用于公共属性或字段;不过,您可以简化为自动实现的属性: public string MajorVersion {get;set;}

但是,如果您只想保存单个字符串值 - xml 是否过度杀伤力? File.WriteAllText / File.ReadAllText 可能更简单。

  1. because XmlSerializer is an object serializer, and works from the type definition
  2. sure, that'll work
  3. strictly speaking you don't, but the current version looks fine; XmlSerializer works for public properties or fields; you could simplify to an automatically-implemented property though: public string MajorVersion {get;set;}

But if you only want to save a single string value - is xml overfkill? File.WriteAllText / File.ReadAllText may be simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文