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 - .NET Tutorial
From the course: .NET Essentials: LINQ for XML
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
-
-
-
-
(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)
-
-
-