XmlNodeReader.ReadString Método

Definición

Lee el contenido de un elemento o nodo de texto como una cadena.

public:
 override System::String ^ ReadString();
public override string ReadString();
override this.ReadString : unit -> string
Public Overrides Function ReadString () As String

Devoluciones

El contenido del elemento o del nodo de tipo texto (esto puede incluir CDATA, nodos de texto, etc.). Puede ser una cadena vacía si el lector se coloca en algo distinto de un elemento o nodo de texto, o si no hay más contenido de texto que devolver en el contexto actual.

Note: El nodo de texto puede ser un elemento o un nodo de texto de atributo.

Ejemplos

En el ejemplo siguiente se muestra el contenido de texto de cada uno de los elementos.

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlNodeReader reader = null;

    try
    {
       //Create and load the XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book>" +
                   "<title>Pride And Prejudice</title>" +
                   "<price>19.95</price>" +
                   "<misc/>" +
                   "</book>");

       //Load the XmlNodeReader
       reader = new XmlNodeReader(doc);

       //Parse the XML and display the text content of each of the elements.
       while (reader.Read()){
         if (reader.IsStartElement()){
           if (reader.IsEmptyElement)
                    {
                        Console.WriteLine("<{0}/>", reader.Name);
                    }
                    else
                    {
               Console.Write("<{0}> ", reader.Name);
               reader.Read(); //Read the start tag.
               if (reader.IsStartElement())  //Handle nested elements.
                   Console.Write("\r\n<{0}>", reader.Name);
               Console.WriteLine(reader.ReadString());  //Read the text content of the element.
           }
         }
       }
     }

     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load the XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<book>" & _
                        "<title>Pride And Prejudice</title>" & _
                        "<price>19.95</price>" & _
                        "<misc/>" & _
                        "</book>")
            
            'Load the XmlNodeReader 
            reader = New XmlNodeReader(doc)
            
            'Parse the XML and display the text content of each of the elements.
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.IsEmptyElement Then
                        Console.WriteLine("<{0}/>", reader.Name)
                    Else
                        Console.Write("<{0}> ", reader.Name)
                        reader.Read() 'Read the start tag.
                        If (reader.IsStartElement())  'Handle nested elements.
                          Console.WriteLine()
                          Console.Write("<{0}>", reader.Name)
                        End If
                        Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
                    End If
                End If
            End While
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

Comentarios

Note

Para aprovechar las características más recientes, la práctica recomendada es crear XmlReader instancias mediante la XmlReaderSettings clase y el Create método . Para obtener más información, vea la sección Comentarios de la página de XmlReader referencia.

Si se coloca en un elemento, ReadString concatena todo el texto, espacios en blanco significativos, espacios en blanco y tipos de nodos de sección CData juntos y devuelve los datos concatenados como contenido del elemento. Se detiene cuando se encuentra cualquier marcado. Esto puede ocurrir en un modelo de contenido mixto o cuando se lee una etiqueta final de elemento.

Si se coloca en un nodo similar al texto, ReadString realiza la misma concatenación del nodo de texto a la etiqueta final del elemento. Si el lector se coloca en un nodo de texto de atributo, ReadString tiene la misma funcionalidad que si el lector estuviera situado en la etiqueta de inicio del elemento. Devuelve todos los nodos de texto del elemento concatenado.

Se aplica a