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 - .NET Tutorial
From the course: .NET Essentials: LINQ for XML
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
-
-
-
-
(Locked)
Get an element from XML2m 33s
-
(Locked)
Get an attribute from XML1m 25s
-
(Locked)
Get nodes when using schema namespace3m 32s
-
(Locked)
Get single or multiple elements2m 27s
-
(Locked)
Get child element1m 12s
-
(Locked)
Get child elements1m 58s
-
(Locked)
Use Descendants() to recurse child elements52s
-
(Locked)
Get attributes51s
-
(Locked)
Read element and attribute values1m 32s
-
(Locked)
Change element and attribute values1m 36s
-
(Locked)
Use SetElementValue to add, remove, and modify child elements1m 37s
-
(Locked)
Use SetValue to set with non-string data1m 4s
-
(Locked)
-
-
-