XmlDocument.ReadNode(XmlReader) XmlDocument.ReadNode(XmlReader) XmlDocument.ReadNode(XmlReader) XmlDocument.ReadNode(XmlReader) Method

定義

根據 XmlReader 中的資訊建立一個 XmlNode 物件。Creates an XmlNode object based on the information in the XmlReader. 讀取器必須定位在節點或屬性上。The reader must be positioned on a node or attribute.

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

XML 來源。The XML source

傳回

新的 XmlNode;如果沒有其他節點,則為 nullThe new XmlNode or null if no more nodes exist.

例外狀況

此讀取器置於節點類型上,該類型不會轉譯為有效的 DOM 節點 (例如 EndElement 或 EndEntity)。The reader is positioned on a node type that does not translate to a valid DOM node (for example, EndElement or EndEntity).

範例

下列範例會使用ReadNode來建立新的節點, 然後將新的節點插入檔中。The following example uses ReadNode to create a new node and then inserts the new node into the 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 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檔案作為輸入。The example uses the file, cd.xml, as input.


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

備註

從指定XmlNode的讀取器讀取一個, 並將讀取器置於下一個節點。Reads one XmlNode from the given reader and positions the reader on the next node. 這個方法會建立XmlNode NodeType符合讀取器目前所在之的型別。This method creates the type of XmlNode matching the NodeType on which the reader is currently positioned. (如果讀取器處於初始狀態, ReadNode則會將讀取器前移至第一個節點, 然後在該節點上操作)。(If the reader is in the initial state, ReadNode advances the reader to the first node and then operates on that node.)

如果讀取器位於專案的開頭, ReadNode則會讀取所有屬性和任何子節點, 包括目前節點的結束標記。If the reader is positioned on the start of an element, ReadNode reads all the attributes and any child nodes, up to and including the end tag of the current node. 傳回XmlNode的包含代表所有讀取之專案的子樹狀目錄。The XmlNode returned contains the sub-tree representing everything read. 讀取器位於結束標記的正後方。The reader is positioned immediately after the end tag.

ReadNode也可以讀取屬性, 但在此情況下, 它不會讓讀取器前進至下一個屬性。ReadNode can also read attributes, but in this case it does not advance the reader to the next attribute. 這可讓您撰寫下列C#程式碼:This allows you to write the following C# code:

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

ReadNode會使用屬性值, 但這表示在屬性上ReadNode呼叫之後false, XmlReader.ReadAttributeValue會傳回。ReadNode does consume the attribute value though, which means after calling ReadNode on an attribute, XmlReader.ReadAttributeValue returns false.

給繼承者的注意事項

這個方法具有繼承需求。This method has an inheritance demand. 需要完全信任才能覆寫ReadNode方法。Full trust is required to override the ReadNode method.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

適用於

另請參閱