XmlDocument.ReadNode(XmlReader) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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.XmlNodePublic 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 扩展。