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
回) 。