反序列化期间XML文档(0,0)中存在错误

时间:2011-02-24 01:43:15

标签: c# wpf xmlserializer

我有以下代码用于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文件,它变成空白,一切都被删除了。任何人都可以帮我解决这个加载按钮错误吗?

1 个答案:

答案 0 :(得分:2)

好的解决了,

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

        }

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