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.
Solution: Move attributes and elements - .NET Tutorial
From the course: .NET Essentials: LINQ for XML
Solution: Move attributes and elements
(upbeat music) - [Narrator] I hope you had fun working on this challenge. Here's my solution. Remember your solution may be different from mine. So our goal was to take this existing text in our XML file, these existing elements and attributes, and convert it into a file that looks like this. So we're moving the attributes into elements and we're also creating these new parent elements. As I was working on it, I found myself doing the same steps. I was creating an XML element named red and then an element green and then blue. And then I was adding those to parent element. And then I was removing the existing items. And as a programmer, when I see myself repeating the same steps over and over again, I feel like I should refactor that into a method. So that's what I did. I created a method called MoveToParentElement. So I pass in a parent element, the current element and the new name of the element. And then I can write…
Contents
-
-
-
-
-
-
(Locked)
Use the DumpFormatted extension method1m 32s
-
(Locked)
XDocument compared to XElement2m 40s
-
(Locked)
Create XML structure3m 18s
-
(Locked)
Rename elements and attributes1m 49s
-
(Locked)
Replace elements and attributes1m 30s
-
(Locked)
Remove element1m 26s
-
(Locked)
Add parent element and move elements2m 14s
-
(Locked)
Challenge: Move attributes and elements1m 9s
-
(Locked)
Solution: Move attributes and elements2m 31s
-
(Locked)
-