XmlDocument Save 使文件保持打开状态

发布于 2025-01-07 02:32:45 字数 1176 浏览 0 评论 0原文

我有一个简单的 C# 函数,可以创建一个基本的 XML 文件并保存:

private void CreateXMlFile(string Filename, string Name, string Company)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode licenseNode = doc.CreateElement("license");
            doc.AppendChild(licenseNode);

            XmlNode node = doc.CreateElement("Name");
            node.AppendChild(doc.CreateTextNode(Name));
            licenseNode.AppendChild(node);

            node = doc.CreateElement("Company");
            node.AppendChild(doc.CreateTextNode(Company));
            licenseNode.AppendChild(node);


            doc.Save(Filename);
        }

当我尝试编辑或删除该文件时,我总是收到以下错误:

该进程无法访问该文件,因为该文件正在被另一个进程使用 流程。

XmlDocument 没有任何内置的处置或关闭例程,我想知道如何在以后编辑或删除文件之前强制关闭文件。

我尝试使用 StreamWriter 保存文件:

StreamWriter outStream = System.IO.File.CreateText(outfile);
            outStream.Write(data);
            outStream.Close();

但这并没有产生相同的错误。

您的建议被极大地接受。

谢谢

I have a simple c# function that creates a basic XML file and saves:

private void CreateXMlFile(string Filename, string Name, string Company)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode licenseNode = doc.CreateElement("license");
            doc.AppendChild(licenseNode);

            XmlNode node = doc.CreateElement("Name");
            node.AppendChild(doc.CreateTextNode(Name));
            licenseNode.AppendChild(node);

            node = doc.CreateElement("Company");
            node.AppendChild(doc.CreateTextNode(Company));
            licenseNode.AppendChild(node);


            doc.Save(Filename);
        }

When I try to edit or delete the file I always get following error:

The process cannot access the file because it is being used by another
process.

XmlDocument doesnt have any inbuilt dispose or close routines and wondered how I can force the file to close before later editing or deleting it.

I have tried to save the file using StreamWriter:

StreamWriter outStream = System.IO.File.CreateText(outfile);
            outStream.Write(data);
            outStream.Close();

But this didnt make a difference with the same error.

Your advice is greatly accepted.

Thank you

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

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

发布评论

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

评论(2

我也只是我 2025-01-14 02:32:45

将 Stream 发送到 XmlDocument 的 Save 方法而不是文件名。

    private static void Main(string[] args)
    {
        CreateXMlFile("c:\\test.xml", "testName", "testCompany");
    }

    private static void CreateXMlFile(string Filename, string Name, string Company)
    {
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode licenseNode = doc.CreateElement("license");
        doc.AppendChild(licenseNode);

        XmlNode node = doc.CreateElement("Name");
        node.AppendChild(doc.CreateTextNode(Name));
        licenseNode.AppendChild(node);

        node = doc.CreateElement("Company");
        node.AppendChild(doc.CreateTextNode(Company));
        licenseNode.AppendChild(node);
        StreamWriter outStream = System.IO.File.CreateText(Filename);

        doc.Save(outStream);
        outStream.Close();
    }

我尝试执行上面的代码,它在我这边运行良好。

Send Stream to XmlDocument's Save method instead of file name.

    private static void Main(string[] args)
    {
        CreateXMlFile("c:\\test.xml", "testName", "testCompany");
    }

    private static void CreateXMlFile(string Filename, string Name, string Company)
    {
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode licenseNode = doc.CreateElement("license");
        doc.AppendChild(licenseNode);

        XmlNode node = doc.CreateElement("Name");
        node.AppendChild(doc.CreateTextNode(Name));
        licenseNode.AppendChild(node);

        node = doc.CreateElement("Company");
        node.AppendChild(doc.CreateTextNode(Company));
        licenseNode.AppendChild(node);
        StreamWriter outStream = System.IO.File.CreateText(Filename);

        doc.Save(outStream);
        outStream.Close();
    }

I tried executing above code and it is working fine at my end.

等风也等你 2025-01-14 02:32:45

你的代码没问题。我在我的机器上测试了它,Save()之后没有留下锁。

尝试使用Unlocker(http://www.softpedia.com/get/System/System-Miscellaneous/Unlocker.shtml)来检查您是否真的是持有锁的人。

您使用哪个 .NET 框架?还有一份报告(http://bytes.com/topic/net/answers/467028-xmldocument-save-does-not-close-file-properly)也无法重现。

Your code is fine. I tested it on my machine and there is no lock left after Save().

Try to use Unlocker (http://www.softpedia.com/get/System/System-Miscellaneous/Unlocker.shtml) to check whether you are really the one who holds the lock.

Which .NET framework do you use? Theres also a report (http://bytes.com/topic/net/answers/467028-xmldocument-save-does-not-close-file-properly) which was not reproducable too.

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