XmlDocument.ReadNode(XmlReader) 方法

定義

根據 XmlReader 中的資訊建立一個 XmlNode 物件。 讀取器必須定位在節點或屬性上。

public:
 virtual System::Xml::XmlNode ^ ReadNode(System::Xml::XmlReader ^ reader);
public virtual System.Xml.XmlNode ReadNode (System.Xml.XmlReader reader);
public virtual System.Xml.XmlNode? ReadNode (System.Xml.XmlReader reader);
abstract member ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
override this.ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
Public Overridable Function ReadNode (reader As XmlReader) As XmlNode

參數

reader
XmlReader

XML 來源。

傳回

新的 XmlNode;如果沒有其他節點,則為 null

例外狀況

此讀取器置於節點類型上,該類型不會轉譯為有效的 DOM 節點 (例如 EndElement 或 EndEntity)。

範例

下列範例會使用 ReadNode 來建立新的節點,然後將新節點插入檔中。

#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 a reader.
   XmlTextReader^ reader = gcnew XmlTextReader( "cd.xml" );
   reader->MoveToContent(); //Move to the cd element node.
   
   //Create a node representing the cd element node.
   XmlNode^ cd = doc->ReadNode( reader );
   
   //Insert the new node into the document.
   doc->DocumentElement->AppendChild( cd );
   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 a reader.
    XmlTextReader reader = new XmlTextReader("cd.xml");
    reader.MoveToContent(); //Move to the cd element node.

    //Create a node representing the cd element node.
    XmlNode cd = doc.ReadNode(reader);

    //Insert the new node into the document.
    doc.DocumentElement.AppendChild(cd);

    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 a reader.
        Dim reader As New XmlTextReader("cd.xml")
        reader.MoveToContent() 'Move to the cd element node.
        'Create a node representing the cd element node.
        Dim cd As XmlNode = doc.ReadNode(reader)
        
        'Insert the new node into the document.
        doc.DocumentElement.AppendChild(cd)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

此範例會使用 檔案 cd.xml ,作為輸入。


<!-- sample CD -->
<cd genre='alternative'>
  <title>Americana</title>
  <artist>Offspring</artist>
</cd>

備註

從指定的讀取器讀取其中一個 XmlNode 讀取器,並將讀取器置於下一個節點上。 這個方法會建立比 NodeType 對目前定位讀取器的 類型 XmlNode 。 (如果讀取器處於初始狀態, ReadNode 請將讀取器前進到第一個節點,然後在該節點上操作。)

如果讀取器位於專案的開頭, ReadNode 則會讀取所有屬性和任何子節點,最多並包含目前節點的結束標記。 傳 XmlNode 回的 包含子樹狀結構,代表讀取的所有專案。 讀取器會緊接在結束標籤之後。

ReadNode 也可以讀取屬性,但在此情況下,它不會將讀取器前進到下一個屬性。 這可讓您撰寫下列 C# 程式碼:

XmlDocument doc = new XmlDocument();
while (reader.MoveToNextAttribute())
{
  XmlNode a = doc.ReadNode(reader);
  // Do some more processing.
}

ReadNode 不過會取用屬性值,這表示在呼叫 ReadNode 屬性之後, XmlReader.ReadAttributeValue 會傳 false 回 。

給繼承者的注意事項

這個方法具有繼承需求。 需要完全信任才能覆寫 ReadNode 方法。

此方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於

另請參閱