XmlAttribute.CloneNode(Boolean) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Erstellt ein Duplikat dieses Knotens.
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
Parameter
- deep
- Boolean
true rekursiv die Unterstruktur unter dem angegebenen Knoten zu klonen; false um nur den Knoten selbst zu klonen.
Gibt zurück
Der doppelte Knoten.
Beispiele
Im folgenden Beispiel wird CloneNode ein Attribut zu zwei verschiedenen Elementknoten hinzugefügt.
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
Im Beispiel wird die Datei als 2elems.xmlEingabe 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>
Hinweise
Diese Methode dient als Kopierkonstruktor für Knoten. Der geklonte Knoten hat kein übergeordnetes Element (ParentNode gibt zurück null).
Das Klonen eines nicht angegebenen Attributs gibt ein angegebenes Attribut zurück (Specified gibt truezurück).