XmlValidatingReader.ReadString Método

Definición

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

String

Contenido del nodo de elemento o de texto. Puede ser una cadena vacía si el lector está situado en un nodo que no sea de elemento ni de texto, o si no hay más contenido de texto para devolver en el contexto actual.

Ejemplos

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ txtreader = nullptr;
   XmlValidatingReader^ reader = nullptr;
   try
   {
      
      //Implement the readers.
      txtreader = gcnew XmlTextReader( "elems.xml" );
      reader = gcnew XmlValidatingReader( txtreader );
      
      //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 != nullptr )
            reader->Close();
   }

}

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

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

    try
    {
       //Implement the readers.
       txtreader = new XmlTextReader("elems.xml");
       reader = new XmlValidatingReader(txtreader);

       //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
Imports System.IO
Imports System.Xml

Public Class Sample
   
   Public Shared Sub Main()
      Dim txtreader As XmlTextReader = Nothing
      Dim reader As XmlValidatingReader = Nothing
      
      Try
         'Implement the readers.
         txtreader = New XmlTextReader("elems.xml")
         reader = New XmlValidatingReader(txtreader)
         
         '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

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

Nota

La XmlValidatingReader clase está obsoleta en .NET Framework 2.0. Puede crear una instancia de XmlReader validación 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 referencia de XmlReader.

Si se coloca en un elemento, ReadString concatena todos los tipos de nodos de texto, espacios en blanco significativos, espacios en blanco y CDATA juntos y devuelve los datos concatenados como contenido del elemento. El lector se detiene cuando se encuentra cualquier marcado, incluidos los comentarios y las instrucciones de procesamiento. Esto se podría producir en un modelo de contenido mixto o cuando se lee una etiqueta de fin 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 sistema de lectura está situado en un nodo de texto de atributos, ReadString tiene la misma funcionalidad que si el sistema de lectura estuviera situado en la etiqueta de inicio de elemento. Devuelve todos los nodos de texto de elementos concatenados.

La EntityHandling propiedad determina cómo ReadString funciona de la siguiente manera:

Valor Descripción
ExpandEntities Devuelve el carácter expandido y las entidades generales. Este es el valor predeterminado.
ExpandCharEntities Devuelve el contenido de texto hasta pero no incluye una referencia de entidad general. Esto significa que una entidad general hace que ReadString se detenga. Debe llamar a para recorrer Read paso a paso por paso la referencia de entidad.

Se aplica a

Consulte también