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 Articles