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

查看:57
本文介绍了LINQ to XML是否在查询期间加载整个xml文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

如您所见,我需要获取根元素的属性.
这意味着在xml文件中,我需要的数据可能在第一行,并且查询必须非常快地完成!
但是相反,它会加载整个文档,然后返回该信息!
因此,问题是如何使用任何内容从大型xml文件中读取少量信息?
System.Threading.Tasks名称空间会有用吗?还是创建异步操作?
还是什至有什么技术可以像binary文件一样在该xml文件上使用?
我不知道!请帮帮我!

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不是最佳方法,原因是Xdocument.Load将整个文件加载到内存中.根据MSDN memory usage will be proportional to the size of the file.,您可以使用XMLReader(请检查文档.

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.

这篇关于LINQ to XML是否在查询期间加载整个xml文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆