XmlDocument.ReadNode(XmlReader) Método

Definición

Crea un objeto XmlNode tomando como base la información de XmlReader. El lector debe estar situado en un nodo o en un atributo.

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

Parámetros

reader
XmlReader

Origen XML.

Devoluciones

Nuevo XmlNode o null si no hay más nodos.

Excepciones

El lector está situado en un tipo de nodo que no se traslada a un nodo DOM válido (por ejemplo, EndElement o EndEntity).

Ejemplos

En el ejemplo siguiente se usa ReadNode para crear un nuevo nodo y, a continuación, se inserta el nuevo nodo en el documento.

#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

En el ejemplo se usa el archivo , cd.xmlcomo entrada.


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

Comentarios

Lee uno XmlNode del lector especificado y coloca el lector en el siguiente nodo. Este método crea el tipo de XmlNode coincidencia con el NodeType en el que está colocado el lector actualmente. (Si el lector está en estado inicial, avanza el lector al primer nodo y, a continuación, ReadNode funciona en ese nodo).

Si el lector se coloca en el inicio de un elemento, ReadNode lee todos los atributos y los nodos secundarios, hasta e incluye la etiqueta final del nodo actual. El XmlNode devuelto contiene el subárbol que representa todo lo leído. El lector se coloca inmediatamente después de la etiqueta final.

ReadNode también puede leer atributos, pero en este caso no avanza el lector al siguiente atributo. Esto le permite escribir el código de C# siguiente:

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

ReadNode sin embargo, consume el valor del atributo, lo que significa que después de llamar a ReadNode en un atributo , XmlReader.ReadAttributeValue devuelve false.

Notas a los desarrolladores de herederos

Este método tiene una demanda de herencia. Se requiere plena confianza para invalidar el ReadNode método .

Este método es una extensión Microsoft al Modelo de objetos de documento (DOM).

Se aplica a

Consulte también