XmlValidatingReader.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.

Przykłady

W poniższym przykładzie jest wyświetlana zawartość tekstowa każdego z elementów.

#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

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

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

Uwagi

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

Uwaga

Klasa XmlValidatingReader jest przestarzała w .NET Framework 2.0. Wystąpienie weryfikacji XmlReader można utworzyć przy użyciu XmlReaderSettings klasy i Create metody . Aby uzyskać więcej informacji, zobacz sekcję Uwagi na stronie referencyjnej XmlReader .

Jeśli pozycja jest umieszczona na elemecie, ReadString łączy cały tekst, znaczące odstępy, białe znaki i typy węzłów sekcji CDATA i zwraca połączone dane jako zawartość elementu. Czytelnik zatrzymuje się po napotkaniu jakichkolwiek 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.

Właściwość EntityHandling określa, jak ReadString działa w następujący sposób:

Wartość Opis
RozwińEntities Zwraca rozszerzony znak i jednostki ogólne. Jest to opcja domyślna.
ExpandCharEntities Zwraca zawartość tekstową do wartości , ale nie obejmuje odwołania do ogólnej jednostki. Oznacza to, że jednostka ogólna powoduje zatrzymanie funkcji ReadString. Musisz wywołać metodę Read , aby przejść przez odwołanie do jednostki.

Dotyczy

Zobacz też