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

Solution: Move attributes and elements - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

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