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 XmlNode.Value method.
- Modify an entire set of nodes by replacing the nodes with new nodes. This is done using the XmlNode.InnerXml Property.
- Replace existing nodes with new nodes using the XmlNode.ReplaceChild method.
- Add additional characters to nodes that inherit from the XmlCharacter class using XmlCharacter.AppendData Method, XmlCharacter.InsertData Method, or XmlCharacter.ReplaceData Method.
- Modify the content by removing a range of characters using the DeleteData Method on node types that inherit from XmlCharacterData.
- Update an attribute value using the SetAttribute method. SetAttribute creates a new attribute if one doesn't exist, and updates an attribute value if the attribute already exists.
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 will be changed.
|Node type||Data changed|
|Attribute||The value of the attribute.|
|CDATASection||The content of the CDATASection.|
|Comment||The content of the comment.|
|ProcessingInstruction||The content, excluding the target.|
|Text||The content of the text.|
|XmlDeclaration||The content of the declaration, excluding the |
|Whitespace||The 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.|
|SignificantWhitespace||The 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 XmlDocument.InnerXml Property.
When using the ReplaceChild and RemoveChild methods, the methods return the replaced or removed node. This node can then be reinserted somewhere else in the DOM. The ReplaceChild 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 exception.
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 will throw an InvalidOperationException exception. 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.