0

我的代码在使用 XmlWriter 时崩溃,说它被另一个进程使用。

    private void generateXml(Control receivedControl)
    {
        foreach (Control subCtrl in receivedControl.Controls)
        {
            using (XmlWriter writer = XmlWriter.Create("C:\\ui.xml"))
            {
                writer.WriteStartElement(subCtrl.Name);
                generateXml(subCtrl);
                writer.WriteEndElement();
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            generateXml(c);
        }
    }

这是我调试的错误:

附加信息:该进程无法访问文件“C:\ui.xml”,因为它正被另一个进程使用。

编辑:多亏了你,我设法让递归工作,但它只写了 xml 文件中的最后一个对象,有人知道为什么吗?这是更新的代码:

    private void generateXml(XmlWriter receivedWriter, Control receivedControl)
    {
        receivedWriter.WriteStartElement(receivedControl.Name);
        foreach (Control subCtrl in receivedControl.Controls)
        {
            generateXml(receivedWriter, subCtrl);
        }
        receivedWriter.WriteEndElement();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            using(XmlWriter writer = XmlWriter.Create("c:\\ui.xml", settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Form");
                generateXml(writer, c);
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
    }
4

3 回答 3

3

XmlWriter 访问的文件将被锁定,因此后续尝试读取它会导致错误。

您可以更改函数以将编写器作为参数传递,从而拥有一份副本并希望避免该问题。

private void generateXml(XmlWriter writer, Control receivedControl)
{
    foreach (Control subCtrl in receivedControl.Controls)
    {
            writer.WriteStartElement(subCtrl.Name);
            generateXml(writer, subCtrl);
            writer.WriteEndElement();
    }
}

private void button2_Click(object sender, EventArgs e)
{
    using (XmlWriter writer = XmlWriter.Create("C:\\ui.xml"))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement(this.Name); // This is the document element
        foreach (Control c in this.Controls)
        {
            generateXml(writer, c);
        }
        writer.WriteEndDocument(); // Close any open tags
    }
}

以上是一个样本 - 未经测试。

编辑:更新以包括根元素

于 2012-12-05T15:38:36.437 回答
0

那是因为您XmlWriter writer一次打开了多个实例。

您应该做的是将您的using语句放在递归函数之外,然后将您XmlWriter

using (XmlWriter writer = XmlWriter.Create("C:\\ui.xml"))
{
    generateXml(c, writer);
于 2012-12-05T15:38:26.623 回答
0

您还可以使用共享写访问权限的文件

        using (var f = new FileStream("C:\\ui.xml",      FileMode.Append,FileAccess.Write,FileShare.Write))
            {
                using (XmlWriter writer = XmlWriter.Create(f))                 
                {
                    writer.WriteStartElement(subCtrl.Name);
                    generateXml(subCtrl);
                    writer.WriteEndElement();
                }
            }
于 2012-12-05T15:47:18.913 回答