From the course: .NET Essentials: LINQ for XML
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Replace elements and attributes - .NET Tutorial
From the course: .NET Essentials: LINQ for XML
Replace elements and attributes
- [Instructor] For a more complete overhaul of an element don't rename, use ReplaceWith. Here's the syntax. We're finding an element named Monster and then we're replacing it with a new element. The name of the element will be Rogue. And that element has one attribute and two child elements. So here's the original element. Here's the replacement. You can do a similar action with an attribute, but the syntax is different. With attributes, there's a method called ReplaceAttributes. That's on the XElement class. You call ReplaceElements and this will replace all the existing attributes with new contents. So right now, there are five attributes on this card element. When I call ReplaceAttributes, and I'm only creating one new attribute here, it's going to replace everything. And you see that down here. Now there's only one attribute. If you would like to add more than one attribute, use an overload of…
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
-
-