XmlDocument.ImportNode(XmlNode, Boolean) 方法

定义

将节点从另一个文档导入到当前文档。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

参数

node
XmlNode

正在被导入的节点。The node being imported.

deep
Boolean

如果执行深层克隆,则为 true;否则为 falsetrue to perform a deep clone; otherwise, false.

返回

导入的 XmlNodeThe imported XmlNode.

例外

在无法导入的节点类型上调用此方法。Calling this method on a node type which cannot be imported.

示例

下面的示例将一个 book 节点从另一个 XML 文档导入到原始 XML 文档中。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

该示例使用文件(books.xml)作为输入。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>

注解

返回的节点没有父级。The returned node has no parent. 源节点未更改或已从原始文档中删除;ImportNode 创建源节点的副本。The source node is not altered or removed from the original document; ImportNode creates a copy of the source node.

导入节点会创建一个 XmlNode 对象,该对象由导入文档所拥有,并且 NameNodeType 与源节点相同。Importing a node creates an XmlNode object owned by the importing document, with Name and NodeType identical to the source node. 新对象还包含与命名空间(PrefixLocalNameNamespaceURI)相关的属性。The new object also has the attributes related to namespaces (Prefix, LocalName, and NamespaceURI).

根据导入节点的节点类型和 deep 参数的值,将根据需要复制其他信息。Depending on the node type of the imported node and the value of the deep parameter, additional information is copied as appropriate. 如果将 XML 或 HTML 源的片段从一个文档复制到另一个文档(认识到 XML 情况下,这两个文档可能具有不同的 Dtd),则此方法会尝试镜像预期的行为。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).

下表描述了每个 XmlNodeType的特定行为。The following table describes the specific behavior for each XmlNodeType.

XmlNodeTypeXmlNodeType ImportNode (true)ImportNode(true) ImportNode(false)ImportNode(false)
特性Attribute Specified 属性设置为生成的 XmlAttribute上的 trueThe Specified property is set to true on the generated XmlAttribute. 将以递归方式导入源 XmlAttribute 的后代,并重组生成的节点,以构成相应的子树。The descendants of the source XmlAttribute are recursively imported and the resulting nodes reassembled to form the corresponding subtree. deep 参数不适用于 XmlAttribute 节点;导入后,它们始终携带其子代。The deep parameter does not apply to XmlAttribute nodes; they always carry their children with them when imported.
CDataCData 复制该节点,包括复制其数据。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.
DocumentFragmentDocumentFragment 递归导入源节点的子代,并重组生成的节点,以构成相应的子树。The descendants of the source node are recursively imported and the resulting nodes reassembled to form the corresponding subtree. 生成空 XmlDocumentFragmentAn empty XmlDocumentFragment is generated.
DocumentTypeDocumentType 复制该节点,包括复制其数据。*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.
将导入源元素的指定属性节点,并将生成的 XmlAttribute 节点附加到生成的 XmlElementSpecified 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.
EntityReferenceEntityReference 因为源文档和目标文档可能以不同的方式定义实体,所以此方法只复制 XmlEntityReference 的节点。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. 因为源文档和目标文档可能以不同的方式定义实体,所以此方法只复制 XmlEntityReference 的节点。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.
ProcessingInstructionProcessingInstruction 从导入的节点复制目标和数据值。Copies the target and data value from the imported node. 从导入的节点复制目标和数据值。Copies the target and data value from the imported node.
TextText 复制该节点,包括复制其数据。Copies the node, including its data. 复制该节点,包括复制其数据。Copies the node, including its data.
SignificantWhitespaceSignificantWhitespace 复制该节点,包括复制其数据。Copies the node, including its data. 复制该节点,包括复制其数据。Copies the node, including its data.
WhitespaceWhitespace 复制该节点,包括复制其数据。Copies the node, including its data. 复制该节点,包括复制其数据。Copies the node, including its data.
XmlDeclarationXmlDeclaration 从导入的节点复制目标和数据值。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.

* 尽管可以导入 DocumentType 的节点,但文档只能有一个 DocumentType。*Although DocumentType nodes can be imported, a document can only have one DocumentType. 如果文档当前具有 DocumenType 节点,则必须先将其删除,然后再添加一个新节点。If the document currently has a DocumenType node, it must be removed before adding a new one.

适用于