From the course: .NET Essentials: LINQ for XML

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use Descendants() to recurse child elements

Use Descendants() to recurse child elements - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

Use Descendants() to recurse child elements

- [Instructor] The elements method only finds those elements that are direct descendants or immediate children of the element you're looking at. So in this case, this method call here will only find the direct descendants of the root element. This call will only find the direct descendants of the card element, and so on. So the deeper my XML tree, the longer this line of code will get. Now to simplify this, there is another technique or another method called descendants, which finds children at any level, grandchildren, children, and so on. So that shortens the line of code, and I get exactly the same results. Now, there is one caveat to this. You need to know about this, is this will find it no matter how deep it is on any branch of the tree. So anywhere that there is an EyeCount element, you'll find it. That may be what you want, or you may not want some EyeCount or some element names that is in a different branch.

Contents