Uredi

Deli z drugimi prek


XmlDocument.ImportNode(XmlNode, Boolean) Method

Definition

Imports a node from another document to the current document.

public:
 virtual System::Xml::XmlNode ^ ImportNode(System::Xml::XmlNode ^ node, bool deep);
public virtual System.Xml.XmlNode ImportNode (System.Xml.XmlNode node, bool deep);
abstract member ImportNode : System.Xml.XmlNode * bool -> System.Xml.XmlNode
override this.ImportNode : System.Xml.XmlNode * bool -> System.Xml.XmlNode
Public Overridable Function ImportNode (node As XmlNode, deep As Boolean) As XmlNode

Parameters

node
XmlNode

The node being imported.

deep
Boolean

true to perform a deep clone; otherwise, false.

Returns

The imported XmlNode.

Exceptions

Calling this method on a node type which cannot be imported.

Examples

The following example imports a book node from a second XML document into the original XML document.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<bookstore><book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book></bookstore>" );
   
   //Create another XmlDocument which holds a list of books.
   XmlDocument^ doc2 = gcnew XmlDocument;
   doc2->Load( "books.xml" );
   
   //Import the last book node from doc2 into the original document.
   XmlNode^ newBook = doc->ImportNode( doc2->DocumentElement->LastChild, true );
   doc->DocumentElement->AppendChild( newBook );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<bookstore>" +
                "<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>" +
                "</bookstore>");

    //Create another XmlDocument which holds a list of books.
    XmlDocument doc2 = new XmlDocument();
    doc2.Load("books.xml");

    //Import the last book node from doc2 into the original document.
    XmlNode newBook = doc.ImportNode(doc2.DocumentElement.LastChild, true);
    doc.DocumentElement.AppendChild(newBook);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.LoadXml("<bookstore>" & _
                    "<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>" & _
                    "</bookstore>")
        
        'Create another XmlDocument which holds a list of books.
        Dim doc2 As New XmlDocument()
        doc2.Load("books.xml")
        
        'Import the last book node from doc2 into the original document.
        Dim newBook As XmlNode = doc.ImportNode(doc2.DocumentElement.LastChild, True)
        doc.DocumentElement.AppendChild(newBook)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

The example uses the file, books.xml, as input.

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

Remarks

The returned node has no parent. The source node is not altered or removed from the original document; ImportNode creates a copy of the source node.

Importing a node creates an XmlNode object owned by the importing document, with Name and NodeType identical to the source node. The new object also has the attributes related to namespaces (Prefix, LocalName, and NamespaceURI).

Depending on the node type of the imported node and the value of the deep parameter, additional information is copied as appropriate. This method attempts to mirror the behavior expected if a fragment of XML or HTML source was copied from one document to another (recognizing that, in the XML case, the two documents could have different DTDs).

The following table describes the specific behavior for each XmlNodeType.

XmlNodeType ImportNode(true) ImportNode(false)
Attribute The Specified property is set to true on the generated XmlAttribute. The descendants of the source XmlAttribute are recursively imported and the resulting nodes reassembled to form the corresponding subtree. The deep parameter does not apply to XmlAttribute nodes; they always carry their children with them when imported.
CData Copies the node, including its data. Copies the node, including its data.
Comment Copies the node, including its data. Copies the node, including its data.
DocumentFragment The descendants of the source node are recursively imported and the resulting nodes reassembled to form the corresponding subtree. An empty XmlDocumentFragment is generated.
DocumentType Copies the node, including its data.* Copies the node, including its data.*
Element The descendants of the source element and its specified attribute nodes are recursively imported and the resulting nodes reassembled to form the corresponding subtree.

Note: Default attributes are not copied. If the document being imported into defines default attributes for this element name, those are assigned.
Specified attribute nodes of the source element are imported, and the generated XmlAttribute nodes are attached to the generated XmlElement.

Note: Default attributes are not copied. If the document being imported into defines default attributes for this element name, those are assigned.
EntityReference Because the source and destination documents could have the entities defined differently, this method only copies the XmlEntityReference node. The replacement text is not included. If the destination document has the entity defined, its value is assigned. Because the source and destination documents could have the entities defined differently, this method only copies the XmlEntityReference node. The replacement text is not included. If the destination document has the entity defined, its value is assigned.
ProcessingInstruction Copies the target and data value from the imported node. Copies the target and data value from the imported node.
Text Copies the node, including its data. Copies the node, including its data.
SignificantWhitespace Copies the node, including its data. Copies the node, including its data.
Whitespace Copies the node, including its data. Copies the node, including its data.
XmlDeclaration Copies the target and data value from the imported node. Copies the target and data value from the imported node.
All other node types. These node types cannot be imported. These node types cannot be imported.

*Although DocumentType nodes can be imported, a document can only have one DocumentType. If the document currently has a DocumentType node, it must be removed before adding a new one.

Applies to