XmlAttribute.CloneNode-Methode
Erstellt ein Duplikat dieses Knotens.
Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Overrides Function CloneNode ( _
deep As Boolean _
) As XmlNode
'Usage
Dim instance As XmlAttribute
Dim deep As Boolean
Dim returnValue As XmlNode
returnValue = instance.CloneNode(deep)
public override XmlNode CloneNode (
bool deep
)
public:
virtual XmlNode^ CloneNode (
bool deep
) override
public XmlNode CloneNode (
boolean deep
)
public override function CloneNode (
deep : boolean
) : XmlNode
Parameter
- deep
true, wenn die Teilstruktur unter dem angegebenen Knoten rekursiv geklont werden soll, false, wenn nur der Knoten selbst geklont werden soll.
Rückgabewert
Das Knotenduplikat.
Hinweise
Diese Methode wird als Kopierkonstruktor für Knoten verwendet. Für den geklonten Knoten ist kein übergeordnetes Element vorhanden (ParentNode gibt NULL (Nothing in Visual Basic) zurück).
Beim Klonen eines nicht angegebenen Attributs wird ein angegebenes Attribut zurückgegeben (Specified gibt true zurück).
Beispiel
Im folgenden Beispiel wird ein Attribut mithilfe von CloneNode zwei verschiedenen Elementknoten hinzuzufügen.
Imports System
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
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);
}
}
#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 );
}
import System.*;
import System.IO.*;
import System.Xml.*;
public class Sample
{
public static void main(String[] args)
{
//Create an XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("2elems.xml");
//Create an attribute.
XmlAttribute attr;
attr = doc.CreateAttribute("bk", "genre", "urn:samples");
attr.set_Value("novel");
//Add the attribute to the first book.
XmlElement currNode =
(XmlElement)doc.get_DocumentElement().get_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.get_DocumentElement().get_LastChild();
currNode.SetAttributeNode(attr2);
Console.WriteLine("Display the modified XML...\r\n");
XmlTextWriter writer = new XmlTextWriter(Console.get_Out());
writer.set_Formatting(Formatting.Indented);
doc.WriteContentTo(writer);
} //main
} //Sample
Im Beispiel wird die Datei 2elems.xml als Eingabe verwendet.
<!--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>
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
XmlAttribute-Klasse
XmlAttribute-Member
System.Xml-Namespace
XmlElement