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.

Get single or multiple elements

Get single or multiple elements - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

Get single or multiple elements

- An element can have no children, it could have a single child or it could have multiple child elements with the same name. What we seeing so far as syntax we're getting a single element. This is code that we saw earlier. I go to the root element here and then I call the element method. I provide the name of the element I'm looking for, and element returns the first matching element. And then in this prior example, I used next node to walk through the children. Essentially we're walking through their children one by one. There are other ways to accomplish this including the query expressions we'll see in the next chapter. So let's see if you want to get a collection of children instead of just a single item. Here's the syntax. We're getting a single item element to get a collection you call elements. And if I hover over this, you see that this returns an I innumerable of X element. So I provide the name and if there's…

Contents