From the course: .NET Essentials: LINQ for XML
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Add parent element and move elements - .NET Tutorial
From the course: .NET Essentials: LINQ for XML
Add parent element and move elements
- In this example, I'd like to see how we can move elements around in the XML tree. So we're going to start with the elements in one location and we're going to move them to be Children under another element. So here's what we're trying to do. So this is the original data. I have a collectibles root element and there's seven card elements under collectibles. What I'd like to have is collectibles and then cards. Cause eventually in our company, we're going to have different categories of collectibles, not just cards. So I want all the cards to live under this cards element. This is the syntax to do this. So I started off by getting all the existing cards here, starting Windows variable, then I'm going to the root element and I'm adding a new X element called Cards. And then I'm passing in all of the existing card elements, and making those Children. And then I don't need the original element anymore. So this…
Contents
-
-
-
-
-
-
Use the DumpFormatted extension method1m 32s
-
XDocument compared to XElement2m 40s
-
Create XML structure3m 18s
-
Rename elements and attributes1m 49s
-
Replace elements and attributes1m 30s
-
Remove element1m 26s
-
Add parent element and move elements2m 14s
-
Challenge: Move attributes and elements1m 9s
-
Solution: Move attributes and elements2m 31s
-
-