使用 XMLDocument 解析 XML 文件并移动会导致异常
我正在尝试读取 XML 文件并将数据导出到数据库。我使用 XMLDocument 来读取内容。导出内容后,我将文件移动到存档位置。为了避免在移动之前发生名称冲突,我在文件名后附加了时间戳。问题是,当我在某个时间点重复导出相同的 xml 文件时,我收到异常“该进程无法访问该文件,因为它正在被另一个程序使用”。
我的猜测是加载到内存中的xml文档还没有被释放。有什么办法可以避免这个问题吗?
更新
我尝试了与读取和导出 xml 文件相关的所有代码。 我的代码现在只有这些行,
fName = DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
fName = destinationPath + "\\" + fName;
File.Move(sourcePath, fName);
当我第一次运行它时, 它工作正常。然后我等待 2 秒,然后再次尝试导出它,现在它抛出异常
I am trying to read an XML file and export the data to DB. I use XMLDocument to read the contents. After exporting the content i am moving the file to a archive location. To avoid name conflicts before moving i am appending the filename with timestamp. The problem is when i repeatedly export the same xml file at one point in time i am getting an exception "The process cannot access the file because it is being used by another program".
My guess is the xml document loaded in memory is not freed yet. Is there any way to avoid this issue?
UPDATE
I tried all the code related to reading and exporting the xml file. My code now has only these lines
fName = DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
fName = destinationPath + "\\" + fName;
File.Move(sourcePath, fName);
now when i run it first time it works fine. Then i wait for 2 seconds and then try to export it again now it is throwing me an exception
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同一时间点的 xml 不能相同 - 至少毫秒数必须不同。但我想你会得到很多文件。可能你需要改变方式
same xml in one point of time cannot be - at least milliseconds must be different. but i think you will get a lot of files. may be you need change the way