XmlNode.CloneNode(Boolean) 方法
定义
当在派生类中被重写时,创建该节点的副本。Creates a duplicate of the node, when overridden in a derived class.
public:
abstract System::Xml::XmlNode ^ CloneNode(bool deep);
public abstract System.Xml.XmlNode CloneNode (bool deep);
abstract member CloneNode : bool -> System.Xml.XmlNode
Public MustOverride Function CloneNode (deep As Boolean) As XmlNode
参数
- deep
- Boolean
若要递归地克隆指定节点下的子树,则为 true
;若仅克隆节点本身,则为 false
。true
to recursively clone the subtree under the specified node; false
to clone only the node itself.
返回
克隆的节点。The cloned node.
例外
在不能被克隆的节点类型上调用该方法。Calling this method on a node type that cannot be cloned.
示例
下面的示例显示了深层和浅层克隆之间的差异。The following example shows the difference between a deep and shallow clone.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<book ISBN='1-861001-57-5'>"
"<title>Pride And Prejudice</title>"
"<price>19.95</price>"
"</book>" );
XmlNode^ root = doc->FirstChild;
//Create a deep clone. The cloned node
//includes the child nodes.
XmlNode^ deep = root->CloneNode( true );
Console::WriteLine( deep->OuterXml );
//Create a shallow clone. The cloned node does not
//include the child nodes, but does include its attribute.
XmlNode^ shallow = root->CloneNode( false );
Console::WriteLine( shallow->OuterXml );
}
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"<price>19.95</price>" +
"</book>");
XmlNode root = doc.FirstChild;
//Create a deep clone. The cloned node
//includes the child nodes.
XmlNode deep = root.CloneNode(true);
Console.WriteLine(deep.OuterXml);
//Create a shallow clone. The cloned node does not
//include the child nodes, but does include its attribute.
XmlNode shallow = root.CloneNode(false);
Console.WriteLine(shallow.OuterXml);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As New XmlDocument()
doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"<price>19.95</price>" & _
"</book>")
Dim root As XmlNode = doc.FirstChild
'Create a deep clone. The cloned node
'includes the child nodes.
Dim deep As XmlNode = root.CloneNode(True)
Console.WriteLine(deep.OuterXml)
'Create a shallow clone. The cloned node does not
'include the child nodes, but does include its attribute.
Dim shallow As XmlNode = root.CloneNode(False)
Console.WriteLine(shallow.OuterXml)
End Sub
End Class
注解
此方法用作节点的复制构造函数。This method serves as a copy constructor for nodes. 重复节点没有父 (ParentNode 返回 null
) 。The duplicate node has no parent (ParentNode returns null
).
下表描述了每个的特定行为 XmlNodeType 。The following table describes the specific behavior for each XmlNodeType.
XmlNodeTypeXmlNodeType | 用 clonenode (true) CloneNode(true) | 用 clonenode (false) CloneNode(false) |
---|---|---|
AttributeAttribute | 克隆特性节点,包括子节点。Clones the attribute node, including child nodes. | 克隆特性节点,包括子节点。Clones the attribute node, including child nodes. |
CDataCData | 克隆 CData 节点,包括其数据内容。Clones the CData node, including its data content. | 克隆 CData 节点,包括其数据内容。Clones the CData node, including its data content. |
评论Comment | 克隆注释节点,包括其文本内容。Clones the comment node, including its text content. | 克隆注释节点,包括其文本内容。Clones the comment node, including its text content. |
文档Document | 克隆文档节点,包括任何子节点。Clones the document node, including any child nodes. | 克隆文档节点。Clones the document node. |
DocumentFragmentDocumentFragment | 克隆文档片段节点,包括任何子节点。Clones the document fragment node, including any child nodes. | 克隆文档片段节点。Clones the document fragment node. |
DocumentTypeDocumentType | 克隆文档类型节点。Clones the document type node. | 克隆文档类型节点。Clones the document type node. |
元素Element | 克隆元素节点、其属性和任何子节点。Clones the element node, its attributes, and any child nodes. | 克隆元素节点及其属性,包括任何默认属性。Clones the element node and its attributes, including any default attributes. |
实体Entity | 无法克隆 Entity 节点。Entity nodes cannot be cloned. | 无法克隆 Entity 节点。Entity nodes cannot be cloned. |
EntityReferenceEntityReference | 克隆实体引用节点。Clones the entity reference node. 不包括替换文本。The replacement text is not included. | 克隆实体引用节点。Clones the entity reference node. 不包括替换文本。The replacement text is not included. |
表示法Notation | 表示法节点无法克隆。Notation nodes cannot be cloned. | 表示法节点无法克隆。Notation nodes cannot be cloned. |
ProcessingInstructionProcessingInstruction | 克隆处理指令节点,包括其目标和数据。Clones the processing instruction node, including its target and data. | 克隆处理指令节点,包括其目标和数据。Clones the processing instruction node, including its target and data. |
SignificantWhitespaceSignificantWhitespace | 克隆有效的空白节点,包括其数据值。Clones the significant white space node, including its data value. | 克隆有效的空白节点,包括其数据值。Clones the significant white space node, including its data value. |
文本Text | 克隆文本节点,包括其数据值。Clones the text node, including its data value. | 克隆文本节点,包括其数据值。Clones the text node, including its data value. |
空格Whitespace | 克隆空白节点,包括其数据值。Clones the white space node, including its data value. | 克隆空白节点,包括其数据值。Clones the white space node, including its data value. |
XmlDeclarationXmlDeclaration | 克隆 XmlDeclaration 节点,包括其数据值。Clones the XmlDeclaration node, including its data value. | 克隆 XmlDeclaration 节点,包括其数据值。Clones the XmlDeclaration node, including its data value. |
所有其他节点类型。All other node types. | 无法克隆这些节点类型。These node types cannot be cloned. | 无法克隆这些节点类型。These node types cannot be cloned. |