.NET Framework Class Library
XmlDocument..::.ImportNode Method

Imports a node from another document to the current document.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Syntax

Visual Basic (Declaration)
Public Overridable Function ImportNode ( _
    node As XmlNode, _
    deep As Boolean _
) As XmlNode
Visual Basic (Usage)
Dim instance As XmlDocument
Dim node As XmlNode
Dim deep As Boolean
Dim returnValue As XmlNode

returnValue = instance.ImportNode(node, _
    deep)
C#
public virtual XmlNode ImportNode(
    XmlNode node,
    bool deep
)
Visual C++
public:
virtual XmlNode^ ImportNode(
    XmlNode^ node, 
    bool deep
)
JScript
public function ImportNode(
    node : XmlNode, 
    deep : boolean
) : XmlNode

Parameters

node
Type: System.Xml..::.XmlNode
The node being imported.
deep
Type: System..::.Boolean
true to perform a deep clone; otherwise, false.

Return Value

Type: System.Xml..::.XmlNode
The imported XmlNode.
Exceptions

ExceptionCondition
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.

Examples

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);

  }
}
Visual C++
#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 );
}

CPP_OLD
#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.

None
<?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>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Community Content

stuidodrummer
Where's the ouput?
Where's the ouput?
Tags :

Page view tracker