This documentation is archived and is not being maintained.

XmlReader.ReadString Method

When overridden in a derived class, reads the contents of an element or text node as a string.

[Visual Basic]
Public Overridable Function ReadString() As String
public virtual string ReadString();
public: virtual String* ReadString();
public function ReadString() : String;

Return Value

The contents of the element or text node. This can be an empty string if the reader is positioned on something other than an element or text node, or if there is no more text content to return in the current context.

Note   The text node can be either an element or an attribute text node.


Exception Type Condition
XmlException An error occurred while parsing the XML.


If positioned on an element, ReadString concatenates all text, significant white space, white space, and CDATA section nodes together and returns the concatenated data as the element content. It stops when any markup is encountered. This could occur in a mixed content model, or when an element end tag is read.

If positioned on a text node, ReadString performs the same concatenation from the text node to the element end tag. If the reader is positioned on an attribute text node, ReadString has the same functionality as if the reader were position on the element start tag. It returns all the concatenated element text nodes.


[Visual Basic, C#, C++] The following example displays the text content of each element.

[Visual Basic] 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
            '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)
                        Console.Write("<{0}>" + " ", reader.Name)
                        reader.Read() 'Read the start tag.
                        If (reader.IsStartElement())  'Handle nested elements.
                          Console.Write("<{0}>", reader.Name)
                        End If
                        Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
                    End If
                End If
            End While
            If Not (reader Is Nothing) Then
            End If
        End Try
    End Sub 'Main 
End Class 'Sample

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

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

       //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);
               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.

        if (reader != null)
} // End class

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
   XmlTextReader* reader = 0;

      //Load the reader with the XML file.
      reader = new XmlTextReader(S"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(S"<{0}/>", reader->Name);
               Console::Write(S"<{0}> ", reader->Name);
               reader->Read(); //Read the start tag.
               if (reader->IsStartElement())  //Handle nested elements.
                  Console::Write(S"\r\n<{0}>", reader->Name);
               Console::WriteLine(reader->ReadString());  //Read the text content of the element.


      if (reader != 0)

[Visual Basic, C#, C++] The example uses the file, elems.xml, as input.

  <title>Pride And Prejudice</title>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlReader Class | XmlReader Members | System.Xml Namespace