XmlNodeReader.ReadString Método

Definição

Lê o conteúdo de um elemento ou nó de texto como uma cadeia de caracteres.

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

Retornos

O conteúdo do elemento ou nó semelhante a texto (isso pode incluir CDATA, nós de texto e assim por diante). Essa pode ser uma cadeia de caracteres vazia se o leitor estiver posicionado em algo diferente de um elemento ou nó de texto ou se não houver mais conteúdo de texto a ser retornado no contexto atual.

Note: O nó de texto pode ser um elemento ou um nó de texto de atributo.

Exemplos

O exemplo a seguir exibe o conteúdo de texto de cada um dos 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

Comentários

Note

Para aproveitar os recursos mais recentes, a prática recomendada é criar XmlReader instâncias usando a XmlReaderSettings classe e o Create método. Para obter mais informações, consulte a seção Comentários na XmlReader página de referência.

Se posicionado em um elemento, ReadString concatena todo o texto, espaço em branco significativo, espaço em branco e tipos de nó de seção CData juntos e retorna os dados concatenados como o conteúdo do elemento. Ela é interrompida quando qualquer marcação é encontrada. Isso pode ocorrer em um modelo de conteúdo misto ou quando uma marca de extremidade de elemento é lida.

Se posicionado em um nó semelhante a texto, ReadString executa a mesma concatenação do nó de texto para a marca de extremidade do elemento. Se o leitor estiver posicionado em um nó de texto de atributo, ReadString terá a mesma funcionalidade que se o leitor estivesse posicionado na marca de início do elemento. Ele retorna todos os nós de texto do elemento concatenado.

Aplica-se a