XmlTextReader.ReadString Metoda

Definicja

Odczytuje zawartość elementu lub węzła tekstowego jako ciąg.

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

Zwraca

Zawartość elementu lub węzła tekstowego. Może to być pusty ciąg, jeśli czytnik jest umieszczony na czymś innym niż element lub węzeł tekstowy lub jeśli nie ma więcej zawartości tekstowej, która ma zostać zwrócona w bieżącym kontekście.

Note: Węzeł tekstowy może być elementem lub węzłem tekstowym atrybutu.

Wyjątki

Wystąpił błąd podczas analizowania kodu XML.

Podjęto próbę wykonania nieprawidłowej operacji.

Przykłady

W poniższym przykładzie jest wyświetlana zawartość tekstowa każdego elementu.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew 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 != nullptr )
            reader->Close();
   }

}
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

W przykładzie użyto pliku , elems.xmljako danych wejściowych.


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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

Jeśli pozycja jest umieszczona na elemecie, ReadString łączy cały tekst, znaczące odstępy, białe znaki i CData typy węzłów sekcji i zwraca połączone dane jako zawartość elementu. Zatrzymuje się po napotkaniu znaczników, w tym komentarzy i instrukcji przetwarzania. Może się to zdarzyć w modelu zawartości mieszanej lub po odczytaniu tagu końcowego elementu.

Jeśli pozycja jest umieszczona w węźle tekstowym, ReadString wykonuje to samo łączenie z węzła tekstowego do tagu końcowego elementu. Jeśli czytnik jest umieszczony w węźle tekstowym atrybutu, ReadString ma taką samą funkcjonalność, jak gdyby czytnik znajdował się na tagu start elementu. Zwraca wszystkie łączone węzły tekstowe elementu.

Dotyczy

Zobacz też