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