XmlTextReader.ReadString Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Reads the contents of an element or a text node as a string.
public:
override System::String ^ ReadString();
public override string ReadString ();
override this.ReadString : unit -> string
Public Overrides Function ReadString () As String
Returns
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.
Exceptions
An error occurred while parsing the XML.
An invalid operation was attempted.
Examples
The following example displays the text content of each element.
#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
The example uses the file, elems.xml
, as input.
<book>
<title>Pride And Prejudice</title>
<price>19.95</price>
<misc/>
</book>
Remarks
Note
Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.
If positioned on an element, ReadString
concatenates all text, significant white space, white space, and CData
section node types together and returns the concatenated data as the element content. It stops when any markup is encountered, including comments and processing instructions. 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.