在 DOM 中建立新節點Create New Nodes in the DOM

XmlDocument 有一個適用於所有節點型別的建立方法。The XmlDocument has a create method for all of the node types. 必要時請為該方法提供名稱,並針對具有內容的節點 (例如,文字節點) 提供內容或其他參數,即可建立節點。Supply the method with a name when required, and content or other parameters for those nodes that have content (for example, a text node), and the node is created. 下列方法需要填入名稱及一些其他參數來建立適當的節點。The following methods are ones that need a name and a few other parameters filled to create an appropriate node.

其他節點型別除了需為參數提供資料以外,還有更多需求。Other node types have more requirements than just providing data to parameters.

如需屬性的詳細資訊,請參閱為 DOM 中的項目建立新屬性For information on attributes, see Creating New Attributes for Elements in the DOM. 如需項目及屬性名稱驗證的詳細資訊,請參閱建立新節點時的 XML 項目和屬性名稱驗證For information on element and attribute name validation, see XML Element and Attribute Name Verification when Creating New Nodes. 若要建立實體參考,請參閱建立新實體參考For creating entity references, see Creating New Entity References. 如需命名空間如何影響實體參考之擴充的詳細資訊,請參閱命名空間對包含項目和屬性的新節點之實體參考擴充的影響For information on how namespaces affect the expansion of entity references, see Namespace Affect on Entity Reference Expansion for New Nodes Containing Elements and Attributes.

一旦建立新節點,即有數個方法可用來將其插入樹狀結構。Once new nodes are created, there are several methods available to insert them into the tree. 該表格會列出這些方法,並說明新節點在 XML 文件物件模型 (DOM) 中會出現於何處。The table lists the methods with a description of where the new node appears in the XML Document Object Model (DOM).

方法Method 節點取代Node placement
InsertBefore 在參考節點之前插入。Inserted before the reference node. 例如,若要在位置 5 插入新節點:For example, to insert the new node in position 5:

Dim refChild As XmlNode = node.ChildNodes(4) 'The reference is zero-based.node.InsertBefore(newChild, refChild);

XmlNode refChild = node.ChildNodes[4]; //The reference is zero-based. node.InsertBefore(newChild, refChild);

如需詳細資訊,請參閱 InsertBefore 方法。For more information, see the InsertBefore method.
InsertAfter 在參考節點之後插入。Inserted after the reference node. 例如:For example:

node.InsertAfter(newChild, refChild)

node.InsertAfter(newChild, refChild);

如需詳細資訊,請參閱 InsertAfter 方法。For more information, see the InsertAfter method.
AppendChild 將節點加入至指定節點之子節點的清單結尾處。Adds the node to the end of the list of child nodes for the given node. 如果加入的節點為 XmlDocumentFragment,則文件片段的全部內容都會移至此節點的子清單中。If the node being added is an XmlDocumentFragment, the entire contents of the document fragment are moved into the child list of this node. 如需詳細資訊,請參閱 AppendChild 方法。For more information, see the AppendChild method.
PrependChild 將節點加入至指定節點之子節點的清單開頭。Adds the node to the beginning of the list of child nodes of the given node. 如果加入的節點為 XmlDocumentFragment,則文件片段的全部內容都會移至此節點的子清單中。If the node being added is an XmlDocumentFragment, the entire contents of the document fragment are moved into the child list of this node. 如需詳細資訊,請參閱 PrependChild 方法。For more information, see the PrependChild method.
Append XmlAttribute 節點附加至與項目關聯之屬性集合的結尾。Appends an XmlAttribute node to the end of the attribute collection associated with an element. 如需詳細資訊,請參閱 Append 方法。For more information, see the Append method.

另請參閱See also