Modifying Nodes, Content, and Values in an XML Document


There are many ways you can modify the nodes and content in a document. You can:

  • Change the value of nodes using the Value property.

  • Modify an entire set of nodes by replacing the nodes with new nodes. This is done using the InnerXml property.

  • Replace existing nodes with new nodes using the RemoveChild method.

  • Add additional characters to nodes that inherit from the XmlCharacterData class using the AppendData, InsertData, or ReplaceData methods.

  • Modify the content by removing a range of characters using the DeleteData method on node types that inherit from XmlCharacterData.

A simple technique for changing the value of a node is to use node.Value = "new value";. The following table lists the node types that this single line of code works on and exactly what data for that node type is changed.

Node typeData changed
AttributeThe value of the attribute.
CDATASectionThe content of the CDATASection.
CommentThe content of the comment.
ProcessingInstructionThe content, excluding the target.
TextThe content of the text.
XmlDeclarationThe content of the declaration, excluding the <?xml and ?> markup.
WhitespaceThe value of the white space. You can set the value to be one of the four recognized XML white space characters: space, tab, CR, or LF.
SignificantWhitespaceThe value of the significant white space. You can set the value to be one of the four recognized XML white space characters: space, tab, CR, or LF.

Any node type not listed in the table is not a valid node type to set a value on. Setting a value on any other node type throws an InvalidOperationException.

The InnerXml property changes the markup of the child nodes for the current node. Setting this property replaces the child nodes with the parsed contents of the given string. The parsing is done in the current namespace context. In addition, InnerXml removes redundant namespace declarations. As a result, numerous cut and paste operations do not increase the size of your document with redundant namespace declarations. For a code example showing the effect of namespaces on the InnerXml operation, see the InnerXml property.

When using the ReplaceData and RemoveChild methods, the methods return the replaced or removed node. This node can then be reinserted somewhere else in the XML Document Object Model (DOM). The ReplaceData method does two validation checks on the node being inserted into the document. The first check ensures that the node is becoming a child of a node that can have child nodes of its type. The second check ensures that the node being inserted is not an ancestor of the node it is becoming a child of. Violating either of these conditions throws an InvalidOperationException.

It is valid to add or remove a read-only child from a node that can be edited. However, attempts to modify the read-only node itself throws an InvalidOperationException. An example of this is modifying the children of an XmlEntityReference node. The children are read-only and cannot be modified. Any attempt to modify them throws an InvalidOperationException.

XML Document Object Model (DOM)