反序列化期间 XML 文档 (0, 0) 出现错误

发布于 2024-10-18 23:21:20 字数 2809 浏览 4 评论 0原文

我有以下用于 xml 序列化的代码。

    public class FormSaving
    {
        private string major;

        public string Majorversion
        {
            get;

            set;

        }
    }



    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;

        FormSaving abc = new FormSaving();
        abc.Majorversion = MajorversionresultLabel.Content.ToString();
        using (Stream savestream = new FileStream(savepath, FileMode.Create))
        {

                XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                serializer.Serialize(savestream, abc);
        }



    }


    private void LoadButton_Click(object sender, RoutedEventArgs e)
    {


        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;
                    checkStream.Close();
                }
            }
            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");
        }

        FormSaving abc;
        using (Stream loadstream = new FileStream(loadpath, FileMode.Create))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
            abc = (FormSaving)serializer.Deserialize(loadstream);

        }

        MajorversionresultLabel.Content = abc.Majorversion;
    }

当我按下 SaveButton 时,我的 label.content 被保存到一个 xml 文件中。但是,当我按下加载按钮加载此 xml 文件时,我收到错误“XML 文档 (0, 0) 中存在错误”。按下加载按钮后,我去打开我的 xml 文件,它变成空白,所有内容都被删除了。谁能帮我解决这个加载按钮错误?

i have the following code to for xml serialization.

    public class FormSaving
    {
        private string major;

        public string Majorversion
        {
            get;

            set;

        }
    }



    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;

        FormSaving abc = new FormSaving();
        abc.Majorversion = MajorversionresultLabel.Content.ToString();
        using (Stream savestream = new FileStream(savepath, FileMode.Create))
        {

                XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                serializer.Serialize(savestream, abc);
        }



    }


    private void LoadButton_Click(object sender, RoutedEventArgs e)
    {


        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;
                    checkStream.Close();
                }
            }
            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");
        }

        FormSaving abc;
        using (Stream loadstream = new FileStream(loadpath, FileMode.Create))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
            abc = (FormSaving)serializer.Deserialize(loadstream);

        }

        MajorversionresultLabel.Content = abc.Majorversion;
    }

When i press the SaveButton, my label.content is saved into an xml file. However when i press the load button to load this xml file, i get the error "There is an error in XML document (0, 0)". I went to open my xml file after pressing the load button, it becomes blank and everything got erased. Can anyone help me fix this load button error?

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

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

发布评论

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

评论(1

左耳近心 2024-10-25 23:21:20

好的解决了,

using (Stream loadstream = new FileStream(loadpath, FileMode.Open))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
            abc = (FormSaving)serializer.Deserialize(loadstream);

        }

应该是 FileMode.Open 而不是 FileMode.Create

ok solved,

using (Stream loadstream = new FileStream(loadpath, FileMode.Open))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
            abc = (FormSaving)serializer.Deserialize(loadstream);

        }

should have been FileMode.Open instead of FileMode.Create

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