Export (0) Print
Expand All

XmlDocument.ImportNode Method

Imports a node from another document to the current document.

[Visual Basic]
Public Overridable Function ImportNode( _
   ByVal node As XmlNode, _
   ByVal deep As Boolean _
) As XmlNode
[C#]
public virtual XmlNode ImportNode(
 XmlNode node,
 bool deep
);
[C++]
public: virtual XmlNode* ImportNode(
 XmlNode* node,
 bool deep
);
[JScript]
public function ImportNode(
   node : XmlNode,
 deep : Boolean
) : XmlNode;

Parameters

node
The node being imported.
deep
true to perform a deep clone; otherwise, false.

Return Value

The imported XmlNode.

Exceptions

Exception Type Condition
InvalidOperationException Calling this method on a node type which cannot be imported.

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 DocumenType node, it must be removed before adding a new one.

Example

[Visual Basic, C#, C++] The following example imports a book node from a second XML document into the original XML document.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample

[C#] 
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);

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<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(S"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(S"Display the modified XML...");
    doc->Save(Console::Out);
}

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>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace

Show:
© 2014 Microsoft