XML序列化IOexception未处理

时间:2011-02-23 07:16:32

标签: c# wpf xmlserializer

我有以下代码用于序列化标签的内容。当我按下“保存”按钮时,会生成一个xml文件。在我选择相同的xml文件后按“加载”按钮时,发生错误,IOexception未处理,进程无法访问文件'C:\ datasaved.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();
        FileStream 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;
                }
            }
            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;
        FileStream loadstream = new FileStream(loadpath, FileMode.Open);
        XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
        abc = (FormSaving)serializer.Deserialize(loadstream);
        loadstream.Close();
        MajorversionresultLabel.Content = abc.Majorversion;
    }

1 个答案:

答案 0 :(得分:3)

这是最直接的问题:

FileStream savestream = new FileStream(savepath, FileMode.Create);
XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
serializer.Serialize(savestream, abc);

您没有关闭该流,因此无法重新打开该文件以进行读取。使用using声明:

using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
    XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
    serializer.Serialize(savestream, abc);
}

加载文件时也应采用相同的方法,而不是使用当前代码显式调用Close,如果在反序列化时发生异常,则表示您赢了关闭流。

通过Dialog.OpenFile打开文件,但不关闭该流...为什么要打开它两次呢?只需从您打开的流中读取。

最后(目前)你正在捕捉异常(盲目地,不考虑哪些异常真的值得尝试处理)但是然后继续无论如何。如果你遇到了异常,那么方法的最后一部分可能无法正确执行,所以你应该 返回自己抛出另一个异常。