XmlDocument.ImportNode(XmlNode, Boolean) 方法

定義

從其他文件匯入節點至目前的文件。

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

匯入的節點。

deep
Boolean

true 以執行深層複製;否則為 false

傳回

XmlNode

匯入的 XmlNode

例外狀況

在無法匯入的節點類型上呼叫這個方法。

範例

下列範例會將書籍節點從第二個 XML 檔匯入原始 XML 檔。

#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 ,作為輸入。

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

備註

傳回的節點沒有父節點。 原始檔案不會改變或移除來源節點; ImportNode 會建立來源節點的複本。

匯入節點會 XmlNode 建立匯入檔擁有的物件,且與 Name NodeType 來源節點相同。 新的 物件也有與命名空間相關的屬性, Prefix (、 LocalName 和) NamespaceURI

視匯入節點的節點類型和參數的值 deep 而定,會視需要複製其他資訊。 如果 XML 或 HTML 來源片段從一份檔案複製到另一份檔,則此方法會嘗試鏡像預期的行為, (在 XML 案例中,這兩份檔可能會有不同的 DTD) 。

下表描述每個 XmlNodeType 的特定行為。

XmlNodeType ImportNode (true) ImportNode (false)
屬性 屬性 Specified 會在產生的 XmlAttribute 上設定為 true 。 來源 XmlAttribute 的子系會以遞迴方式匯入,而產生的節點會重新組合成對應的子樹。 參數 deep 不適用於 XmlAttribute 節點;它們一律會在匯入時搭配其子系。
CData 複製節點,包含其資料。 複製節點,包含其資料。
註解 複製節點,包含其資料。 複製節點,包含其資料。
DocumentFragment 來源節點的子代會遞迴匯入,而且產生的節點會重組以形成對應的樹狀子目錄。 會產生空 XmlDocumentFragment 的 。
DocumentType 複製節點,包含其 data.* 複製節點,包含其 data.*
項目 來源專案的子系及其指定的屬性節點會以遞迴方式匯入,而產生的節點會重新組合成對應的子樹。

注意:不會複製預設屬性。 如果要匯入的文件定義這個項目名稱的預設屬性,就會指派這些屬性。
來源專案的指定屬性節點會匯入,而產生的 XmlAttribute 節點會附加至產生的 XmlElement

注意:不會複製預設屬性。 如果要匯入的文件定義這個項目名稱的預設屬性,就會指派這些屬性。
EntityReference 因為來源和目的地檔可能有不同的定義實體,所以此方法只會複製 XmlEntityReference 節點。 不包括取代文字。 如果目的文件有定義的實體,就會指派它的值。 因為來源和目的地檔可能有不同的定義實體,所以此方法只會複製 XmlEntityReference 節點。 不包括取代文字。 如果目的文件有定義的實體,就會指派它的值。
ProcessingInstruction 從匯入的節點複製目標和資料值。 從匯入的節點複製目標和資料值。
Text 複製節點,包含其資料。 複製節點,包含其資料。
SignificantWhitespace 複製節點,包含其資料。 複製節點,包含其資料。
空白 複製節點,包含其資料。 複製節點,包含其資料。
XmlDeclaration 從匯入的節點複製目標和資料值。 從匯入的節點複製目標和資料值。
所有其他節點類型。 這些節點型別不會匯入。 這些節點型別不會匯入。

*雖然可以匯入 DocumentType 節點,但檔只能有一個 DocumentType。 如果檔目前有 DocumentType 節點,必須先移除它,才能新增新的節點。

適用於