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 child element

Get child element - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

Get child element

- [Instructor] XML is hierarchical in nature. Therefore, we need a way to navigate to children elements many levels below the root element. Take a look at line 11. That is the Slogan element. It is a child of Card, which is a child of Collectibles. I can even go further down the tree. Here on line 22 I have the Antenna element, which is a child of Monster, which is a child of Card. This is the syntax to move through the tree. You get a reference to one of the elements. In this case, I get a reference to Card by calling the Element method, and then I just continue calling Element. So now I'm calling Element again to get the Slogan child element, or here on line 12, I'm going to Card, to Monster, and then to Antenna. So there is the Slogan value and there is the Antenna value. Now, if you continue doing this in the tree, this can get verbose and you can get some long lines of text. So one technique I like to use is to…

Contents