Modifying Elements, Attributes, and Nodes in an XML Tree

The following table summarizes the methods and properties that you can use to modify an element, its child elements, or its attributes.

The following methods modify an XElement.

Method

Description

XElement.Parse

Replaces an element with parsed XML.

XElement.RemoveAll

Removes all content (child nodes and attributes) of an element.

XElement.RemoveAttributes

Removes the attributes of an element.

XElement.ReplaceAll

Replaces all content (child nodes and attributes) of an element.

XElement.ReplaceAttributes

Replaces the attributes of an element.

XElement.SetAttributeValue

Sets the value of an attribute. Creates the attribute if it doesn't exist. If the value is set to null, removes the attribute.

XElement.SetElementValue

Sets the value of a child element. Creates the element if it doesn't exist. If the value is set to null, removes the element.

XElement.Value

Replaces the content (child nodes) of an element with the specified text.

XElement.SetValue

Sets the value of an element.

The following methods modify an XAttribute.

Method

Description

XAttribute.Value

Sets the value of an attribute.

XAttribute.SetValue

Sets the value of an attribute.

The following methods modify an XNode (including an XElement or XDocument).

Method

Description

XNode.ReplaceWith

Replaces a node with new content.

The following methods modify an XContainer (an XElement or XDocument).

Method

Description

XContainer.ReplaceNodes

Replaces the children nodes with new content.

See Also

Other Resources

Modifying XML Trees (LINQ to XML)