7.4. Change the Value of an Element or Attribute

Problem

You need to modify an XML document by changing the value of an element or attribute.

Solution

Use one of the available set methods (SetValue, SetAttributeValue, or SetElementValue) of the XElement class.

How It Works

The XElement class provides the following methods for changing the value of elements and attributes in an existing XML tree:

  • SetValue converts the specified value to a String and then assigns it to the Value property of the current XElement instance. This method is also available to the XAttribute class.

  • SetAttributeValue converts the specified value to a String and then assigns it to the Value property of the attribute specified by the provided XName parameter.

  • SetElementValue ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.