修改 XML 文档中的节点、内容和值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:

更改节点值的一个简单方法是使用 node.Value = "new value";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 type 更改的数据Data changed
属性Attribute 属性的值。The value of the attribute.
CDATASectionCDATASection CDATA 节的内容。The content of the CDATASection.
备注Comment 注释的内容。The content of the comment.
ProcessingInstructionProcessingInstruction 内容(不包括目标)。The content, excluding the target.
文本Text 文本的内容。The content of the text.
XmlDeclarationXmlDeclaration 声明的内容,不包括 <?xml?> 标记。The content of the declaration, excluding the <?xml and ?> markup.
WhitespaceWhitespace 空白的值。The value of the white space. 可以将该值设置为四个可识别的 XML 空白字符之一:空格、制表符、CR 或 LF。You can set the value to be one of the four recognized XML white space characters: space, tab, CR, or LF.
SignificantWhitespaceSignificantWhitespace 有效空白的值。The value of the significant white space. 可以将该值设置为四个可识别的 XML 空白字符之一:空格、制表符、CR 或 LF。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. 设置任何其他节点类型的值都将引发 InvalidOperationExceptionSetting a value on any other node type throws an InvalidOperationException.

InnerXml 属性更改当前节点的子节点标记。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. 此外,InnerXml 移除多余的命名空间声明。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. 有关显示命名空间对 InnerXml 操作的影响的代码示例,请参见 InnerXml 属性。For a code example showing the effect of namespaces on the InnerXml operation, see the InnerXml property.

当使用 ReplaceDataRemoveChild 方法时,这两个方法返回已替换或移除的节点。When using the ReplaceData and RemoveChild methods, the methods return the replaced or removed node. 此节点可以重新插入 XML 文档对象模型 (DOM) 中的任何其他位置。This node can then be reinserted somewhere else in the XML Document Object Model (DOM). ReplaceData 方法对插入到文档中的节点执行两个验证检查。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. 违犯这两个条件中的任何一个都将引发 InvalidOperationExceptionViolating 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. 然而,试图修改只读节点本身将引发 InvalidOperationException 异常。However, attempts to modify the read-only node itself throws an InvalidOperationException. 修改 XmlEntityReference 节点的子级便属于这种情况。An example of this is modifying the children of an XmlEntityReference node. 该子级是只读的,因此无法修改。The children are read-only and cannot be modified. 任何修改它们的尝试都将引发 InvalidOperationExceptionAny attempt to modify them throws an InvalidOperationException.

另请参阅See also