tags:

views:

24

answers:

1

Say I have this:

<parent>
  <child name="1" />
  <child name="2" />
  ...
  <child name="8000001" />
  <child name="8000002" />
  <child name="8000003" />
  <child name="8000004" />
  <child name="8000005" />
</parent>

How do I read the last five child elements? Due to the size of the file it is not possible to use XElement.Parse(...) etc. The file needs to be read as a stream using XmlReader.

+1  A: 

You could keep a circular buffer - add each element to the buffer as you parse it (and as you won't be parsing the whole thing, you can use XElement.Load with the reader positioned at the start of the element) and throw away old elements as you read new ones. You'll create an awful lot of garbage, but with any luck most of it will be in gen0, so won't cause too many problems.

I don't know of any circular buffer classes within the main framework, but you could either write a general-purpose one yourself, find a third-party library, or just hard-code the circularity within your reading code.

(Heck, you could just use a Queue<T> and dequeue elements as required... that uses a circular buffer internally, I believe.)

Jon Skeet