【问题标题】:XmlDocument doesn't work after OpenFileDialogOpenFileDialog 后 XmlDocument 不起作用
【发布时间】:2012-10-07 13:00:43
【问题描述】:

这真的很奇怪。我正在编写一个编写 XML 文件的应用程序。但是,在某些情况下,不会创建/覆盖该文件。

我设法找到了导致它无法写入所需的特定事件,并将其分离到一个独立的程序中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        bool doFileOpenFirst = false;

        if (doFileOpenFirst)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.CheckFileExists = true;
            dialog.Filter = "Image files|*.bmp;*.jpg;*.png";

            dialog.ShowDialog();
        }

        // Just write a trivial XML file
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);// Create the root element

        XmlElement root = doc.CreateElement("Database");
        doc.AppendChild(root);

        doc.Save("Trivial.xml");
    }
}

现在,如果您运行它,您将看到它最初可以工作。 现在使 doFileOpenFirst 为真。在它写入 XML 之前,它会显示一个用于打开文件的对话框。 如果您使用此对话框选择文件(任何文件;不是“Trivial.xml”),之后的 XML 保存将失败。默默。 如果您在 OpenFileDialog 中点击取消,保存将正常工作。

所以这里的文件句柄可能存在一些问题,但解决方法是什么?您会看到强制 Dispose 的 OpenFileDialog 没有帮助。

【问题讨论】:

    标签: c# xml xmldocument openfiledialog


    【解决方案1】:

    我认为您应该将这些打开对话框并将 xml 保存到表单的加载事件中的代码。

    【讨论】:

    • 好的,但是这个例子很快就说明了问题。在实际代码中,OpenFileDialog 和 XmlDocument.Save 都是由用户手动触发的,并且在初始化或加载后很长时间才会发生。
    【解决方案2】:

    您的对话框 (OpenFileDialog) 和您的 XML 保存代码是相互独立的。因此,无论是否显示对话框都不会导致 XML 保存问题,尤其是在对话框中选择其他文件时。

    另外,为了帮助您,我已经检查了您的代码和步骤,并且无论是否使用 OpenFileDialog,XML 都在保存。因此,假设您的问题不取决于打开文件对话框。在您提供的示例中没有问题。

    【讨论】:

    • 与 cacoroto 的响应相同:代码对我来说一直失败,但我被锁定在旧框架 (2.0) 上,因为我在朋友的笔记本电脑上。如果它对您有用,那么这一定是原因。感谢您为我试用代码。
    【解决方案3】:

    似乎工作正常!我尝试并按预期工作。

    打开随机图像文件没有问题:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace StackOverflow
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            bool doFileOpenFirst = true;
    
            if (doFileOpenFirst)
            {
                OpenFileDialog dialog = new OpenFileDialog();
    
                dialog.CheckFileExists = true;
                dialog.Filter = "Image files|*.bmp;*.jpg;*.png";
    
                dialog.ShowDialog();
            }
    
            // Just write a trivial XML file
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);// Create the root element
    
            XmlElement root = doc.CreateElement("Database");
            doc.AppendChild(root);
    
            doc.Save("Trivial.xml");
        }
    }
    }
    

    【讨论】:

    • 刚刚再次运行它仍然对我不起作用。但我是在一台破旧的笔记本电脑上写的,它是 VS2005(因此是 .NET 2.0)。让它在你的机器上工作 cacoroto prob 意味着这是已经修复的问题。谢谢(对不起,如果浪费了任何人的时间)。
    猜你喜欢
    • 2012-02-07
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2014-08-19
    • 1970-01-01
    • 2012-11-25
    • 2018-09-08
    相关资源
    最近更新 更多