LINQ到XML是否在查询期间加载整个XML文档?[英] Does LINQ to XML loads whole xml document during query?

本文是小编为大家收集整理的关于LINQ到XML是否在查询期间加载整个XML文档?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个包含数据库的大型xml文件
400mb是一个大小.
它是使用LINQ本身创建的,它是在10 minutes中完成的!很棒的结果!
但是,为了使用LINQ读取xml文件的粒子信息,它需要20分钟及更多!
试想一下,阅读少量信息需要更多时间,然后写大量信息! 在阅读过程中,它需要调用不是IDisposable的函数XDocument.Load(@"C:\400mb.xml").
因此,当它加载整个XML文档以及当它获取我的小信息时,内存不会清除!
我的目标是阅读"

     XDocument XD1 = XDocument.Load(@"C:\400mb.xml");
     string s1 = XD1.Root.Attribute("AnyAttribute").Value;

您可以看到,我需要获得根元素的属性.
这意味着在xml文件中,我需要的数据可能是在第一行上,并且必须很快完成查询!
但是,它不是加载整个文档,然后返回该信息!
因此,问题是如何使用任何东西从大型xml文件中读取少量信息?
System.Threading.Tasks名称空间会有用吗?或创建异步操作?
或者,即使是任何类型的技术,都可以在XML文件上使用binary文件?
我不知道!请帮我!

推荐答案

Xdocument.Load不是最好的方法,原因是xdocument.load将整个文件加载到内存中.根据MSDN memory usage will be proportional to the size of the file.,您可以使用XMLReader(检查在这里)相反,如果您只是打算搜索XML文档.阅读 MSDN的文档.

本文地址:https://www.itbaoku.cn/post/1556837.html

问题描述

I have a large xml file which contains database!
400mb is a size.
it was created using LINQ itself and it was done in 10 minutes! Great result!
But in order to read a particle information from that xml file using LINQ it need 20 minutes and more!
Just imagine to read a small amount of information needs more time then to write a large information! During read process it needs to call a function XDocument.Load(@"C:\400mb.xml") which is not IDisposable.
So when it will load whole xml document and when it gets my small information, Memory does not clears!
My target is to read "

     XDocument XD1 = XDocument.Load(@"C:\400mb.xml");
     string s1 = XD1.Root.Attribute("AnyAttribute").Value;

As you can see, I need to get an Attribute of the Root Element.
This means that in xml file the data I need might be on a first line and query must be done very quickly!
But instead of this it load whole Document and then returns that information!
So the question is How to read that small amount of information from a large xml file using anything?
Will System.Threading.Tasks namespace be useful? Or create asynchronous operations?
Or is even any kind of technique which will work on that xml file like a binary file?
I don't know! Help me Please!

推荐答案

Xdocument.Load is not the best approach, reason being Xdocument.Load loads the whole file into memory. According to MSDN memory usage will be proportional to the size of the file. You can use XMLReader (Check here) instead if you are just planning to search the XML doc. Read this documentation on MSDN.