XmlDocument Save 使文件保持打开状态
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 Stream 发送到 XmlDocument 的 Save 方法而不是文件名。
我尝试执行上面的代码,它在我这边运行良好。
Send Stream to XmlDocument's Save method instead of file name.
I tried executing above code and it is working fine at my end.
你的代码没问题。我在我的机器上测试了它,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.