修改 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 CDATASection 的內容。The content of the CDATASection.
註解Comment 註解的內容。The content of the comment.
ProcessingInstructionProcessingInstruction 內容 (目標除外)。The content, excluding the target.
TextText 文字的內容。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. 不過,嘗試修改唯讀節點本身會擲回 InvalidOperationExceptionHowever, 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