What is the serializer?
With BinaryFormatter
, that would be very, very tricky.
With xml, you could perhaps pre-process the xml, but that it very tricky.
Other serializers exist, though - for example, with protobuf-net there is little difference between an array/list of items, and a sequence of individual items - so it would be pretty easy to pick of a finite sequence of items without processing the entire array.
Complete protobuf-net example:
[ProtoContract]
class Test {
[ProtoMember(1)]
public int Foo { get; set; }
[ProtoMember(2)]
public string Bar { get; set; }
static void Main() {
Test[] data = new Test[1000];
for (int i = 0; i < 1000; i++) {
data[i] = new Test { Foo = i, Bar = ":" + i.ToString() };
}
MemoryStream ms = new MemoryStream();
Serializer.Serialize(ms, data);
Console.WriteLine("Pos after writing: " + ms.Position); // 10760
Console.WriteLine("Length: " + ms.Length); // 10760
ms.Position = 0;
foreach (Test foo in Serializer.DeserializeItems<Test>(ms,
PrefixStyle.Base128, Serializer.ListItemTag).Take(100)) {
Console.WriteLine(foo.Foo + "\t" + foo.Bar);
}
Console.WriteLine("Pos after reading: " + ms.Position); // 902
}
}
Note that DeserializeItems<T>
is a lazy/streaming API, so it only consumes data from the stream as you iterate over it - hence the LINQ Take(100)
avoids us reading the whole stream.