11

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, but 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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Mikey S.
  • 3,301
  • 6
  • 36
  • 55
  • Ok, after searching a bit more I've bumped into this thread: http://stackoverflow.com/questions/2441673/reading-xml-with-xmlreader-in-c And this: http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx I've been thinking of solving this issue using almost the exact technique, but it still lacks full support and better performance. – Mikey S. Aug 05 '11 at 16:36

2 Answers2

3

Take a look at this codeplex project.

1

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • Thanks, found a few similiar solutions using the technique you mentioned above, but I loved the 3rd party tool jdv posted. – Mikey S. Aug 05 '11 at 20:54