score:12

Accepted answer

well to start with, i don't think you wanted the second descendant overall - you wanted the second chapter, which isn't necessarily the same thing. fortunately it's easy to do:

// use 1 for the second chapter, 2 for "element 2" (i.e. the third element) etc
var chapter = book.descendants("chapter").elementat(1);

var verses = from verse in chapter...

edit: for the verse number, use the overload of select which provides the element index:

var verses = chapter.element("verse")
                    .select((element, index) => new {
                                versenumber = index + 1,
                                text = element.value
                            });

i don't think it makes much sense for every verse to have the first and last verse in the paragraph, personally. it's not really a feature of that verse, is it?

score:0

by using the extension method notation you can use the select() call that will provide you the index of the current element like this:

var indexedchapters = book.decendants().select((chapter, index) => new { chapter, index });

afterwards you can than access the index of the anonymous type in any following linq statement.

score:2

another correct answer, although jon skeet's answer is way more efficient, is this code:

chapter.elementsbeforeself().count()

Related Query

More Query from same tag