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.

範例

下列範例會將第二個 XML 檔中的 book 節點匯入到原始的 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 true產生的XmlAttribute上, 屬性會設定為。The 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. 參數不會套用至XmlAttribute節點, 它們會在匯入時, 一律攜帶其子系。 deepThe 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. 會產生XmlDocumentFragment空的。An empty XmlDocumentFragment is generated.
DocumentTypeDocumentType 複製節點,包含其 data.*Copies the node, including its data.* 複製節點,包含其 data.*Copies the node, including its data.*
元素Element Source 元素及其指定屬性節點的下階會以遞迴方式匯入, 而產生的節點會重組以形成對應的子樹。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的節點會附加至產生XmlElement的。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.
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.

適用於