XmlAttribute.CloneNode(Boolean) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建此节点的副本。
public:
override System::Xml::XmlNode ^ CloneNode(bool deep);
public override System.Xml.XmlNode CloneNode (bool deep);
override this.CloneNode : bool -> System.Xml.XmlNode
Public Overrides Function CloneNode (deep As Boolean) As XmlNode
参数
- deep
- Boolean
若要递归地克隆指定节点下的子树,则为 true
;若仅克隆节点本身,则为 false
。
返回
重复节点。
示例
以下示例使用 CloneNode
将属性添加到两个不同的元素节点。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
//Create an XmlDocument.
XmlDocument^ doc = gcnew XmlDocument;
doc->Load( "2elems.xml" );
//Create an attribute.
XmlAttribute^ attr;
attr = doc->CreateAttribute( "bk", "genre", "urn:samples" );
attr->Value = "novel";
//Add the attribute to the first book.
XmlElement^ currNode = dynamic_cast<XmlElement^>(doc->DocumentElement->FirstChild);
currNode->SetAttributeNode( attr );
//An attribute cannot be added to two different elements.
//You must clone the attribute and add it to the second book.
XmlAttribute^ attr2;
attr2 = dynamic_cast<XmlAttribute^>(attr->CloneNode( true ));
currNode = dynamic_cast<XmlElement^>(doc->DocumentElement->LastChild);
currNode->SetAttributeNode( attr2 );
Console::WriteLine( "Display the modified XML...\r\n" );
XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );
writer->Formatting = Formatting::Indented;
doc->WriteContentTo( writer );
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create an XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("2elems.xml");
//Create an attribute.
XmlAttribute attr;
attr = doc.CreateAttribute("bk","genre","urn:samples");
attr.Value = "novel";
//Add the attribute to the first book.
XmlElement currNode = (XmlElement) doc.DocumentElement.FirstChild;
currNode.SetAttributeNode(attr);
//An attribute cannot be added to two different elements.
//You must clone the attribute and add it to the second book.
XmlAttribute attr2;
attr2 = (XmlAttribute) attr.CloneNode(true);
currNode = (XmlElement) doc.DocumentElement.LastChild;
currNode.SetAttributeNode(attr2);
Console.WriteLine("Display the modified XML...\r\n");
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
doc.WriteContentTo(writer);
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
Dim doc as XmlDocument = new XmlDocument()
doc.Load("2elems.xml")
'Create an attribute.
Dim attr as XmlAttribute
attr = doc.CreateAttribute("bk","genre","urn:samples")
attr.Value = "novel"
'Add the attribute to the first book.
Dim currNode as XmlElement
currNode = CType(doc.DocumentElement.FirstChild, XmlElement)
currNode.SetAttributeNode(attr)
'An attribute cannot be added to two different elements.
'You must clone the attribute and add it to the second book.
Dim attr2 as XmlAttribute
attr2 = CType (attr.CloneNode(true), XmlAttribute)
currNode = CType(doc.DocumentElement.LastChild, XmlElement)
currNode.SetAttributeNode(attr2)
Console.WriteLine("Display the modified XML...")
Dim writer as XmlTextWriter = new XmlTextWriter(Console.Out)
writer.Formatting = Formatting.Indented
doc.WriteContentTo(writer)
end sub
end class
该示例使用 文件 2elems.xml
作为输入。
<!--sample XML fragment-->
<bookstore>
<book ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
<book ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
</bookstore>
注解
此方法用作节点的复制构造函数。 克隆的节点没有父 (ParentNode 返回 null
) 。
克隆未指定的属性将返回指定的属性, Specified (返回 true
) 。