简体   繁体   English

如何使用XmlReader读取XML文档的最后N个元素?

[英]How do I read the last N elements of an XML document using XmlReader?

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. 由于文件的大小,无法使用XElement.Parse(...)等。需要使用XmlReader将文件作为流读取。

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. 您可以保留一个循环缓冲区-在解析每个元素时将其添加到缓冲区中(因为您将不会解析整个内容,因此可以XElement.Load与位于元素开头的阅读器一起使用)并丢弃阅读新元素时的旧元素。 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. 您将创建大量的垃圾,但是如果运气好的话,大多数将在gen0中进行,因此不会造成太多问题。

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.) (相信,您可以根据需要使用Queue<T>并出队元素...我相信内部使用循环缓冲区。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM