XmlTextReader.ReadString Método

Definición

Lee el contenido de un elemento o un 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

Contenido del elemento o nodo de texto. 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.

Excepciones

Error al analizar el XML.

Se intentó una operación no válida.

Ejemplos

En el ejemplo siguiente se muestra el contenido de texto de cada elemento.

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

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

    try
    {
       //Load the reader with the XML file.
       reader = new XmlTextReader("elems.xml");

       //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 XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("elems.xml")
            
            '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

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


<book>
  <title>Pride And Prejudice</title>
  <price>19.95</price>
  <misc/>
</book>

Comentarios

Note

Se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.

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

Si se coloca en un nodo de 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

Consulte también