Modify elements, attributes, and nodes in an XML tree (LINQ to XML)
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: |
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for