开发者

Linq To XML, yield and others

开发者 https://www.devze.com 2023-03-26 08:15 出处:网络
I was wondering if there\'s a .NET library or a 3rd party tool for executing Entity Framework like LINQ queries on XML Documents. I know there\'s already LINQ to XML which allows you to execute querie

I was wondering if there's a .NET library or a 3rd party tool for executing Entity Framework like LINQ queries on XML Documents. I know there's already LINQ to XML which allows you to execute queries on an XDocument object which is ALREADY loaded into memory, b开发者_C百科ut what if the XML Document is extremely large in size (over a gigabyte)?

I would like to have an option to hand this query over to an XmlReader rather to an XDocument object. Is that possible straight out of the box?


Take a look at this codeplex project.


I don't believe you will have a solution that will fit all XML documents, but you can do it.

I would create a class that implements IEnumerable<T> and takes the XmlReader that you want to stream over.

Then, I would create the type that will be used for the type parameter T in your implementation of IEnumerable<T>.

Once you have that, in your implementation of GetEnumerator, you would call the various Move* and Read* methods on the XmlReader which will allow you to construct the single instance of T.

When you have an instance of T in-hand, you would use yield return to yield the item. The rest of the body of GetEnumerator would loop appropriately as you stream through the XmlReader.

With that in hand, you will stream instances of T as you get them, without having to load the entire document into memory in the first place.

You have to test, of course, how much of the document you want to read at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消