在 C# 中使用 XmlTextReader 读取大文件
我正在使用 XmlTextReader 读取一个大文件,需要几个小时才能读取并进行一些处理,在读取和处理期间,如果该文件被更改或从文件服务器中删除,会发生什么?
I am reading a large file with XmlTextReader, it takes hours to read and do some processing, during the reading and processing , what will happen if this file is changed or deleted from file server ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果文件被成功编辑或删除(即您没有将其锁定以供读取),您的应用程序将崩溃(假设您的应用程序认为它已打开该文件)。但更有可能的是:其他进程将无法访问该文件来编辑/删除它。
如果处理需要时间,那么请考虑立即加载数据。然而,如果由于数量庞大而速度缓慢……好吧,对此无能为力(假设数量*处理时间是瓶颈;如果它只是因为xml 文件的大小,替代格式可能会有所帮助)。如果 IO 是您的瓶颈,您可能想要尝试压缩(无论是在文件系统级别,还是在写入时通过 gzip 显式运行您的 xml) - 它可能 (不保证)对于旋转盘片来说,减少物理读取(交换 CPU 时间对其进行解压缩)会稍微加快速度。但对于 SSD 或高性能磁盘来说,情况可能并非如此。
If the file is successfully edited or deleted (i.e. you didn't have it locked for reading), your app will crash (assuming your app thought it had the file open). More likely, though: the other process will not be able to get access to the file to edit/delete it.
If it is the processing that takes time, then consider loading the data eagerly. If, however, it is slow due to sheer volume... well, there isn't much can be done about that (assuming the volume * processing-time is the bottleneck; if it is just because of the size of the xml file, an alternative format might help). If IO is your bottleneck, you might want to try experimenting with compression (either at the file-system level, or explicitly running your xml through gzip when writing) - it might (not guaranteed) be that for a spinning platter, reducing the physical reads (trading CPU time decompressing it) speeds things up a little. Probably not the case for SSD or high-performance disks, though.