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

Replace elements and attributes - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

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