Export (0) Print
Expand All

XmlDocument.ImportNode Method

Imports a node from another document to the current document.

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

public virtual XmlNode ImportNode(
	XmlNode node,
	bool deep
)

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.

ExceptionCondition
InvalidOperationException

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

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.

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

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

  }
}

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>

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft