XmlNodeReader::ReadString Method ()
Reads the contents of an element or text node as a string.
Assembly: System.Xml (in System.Xml.dll)
Return Value
Type: System::String^The contents of the element or text-like node (This can include CDATA, Text nodes, and so on). 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.
Note |
|---|
In the .NET Framework 2.0, the recommended practice is to create XmlReader instances using the XmlReaderSettings class and the Create method. This allows you to take full advantage of all the new features introduced in the .NET Framework. For more information, see the Remarks section in the XmlReader reference page. |
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. This could occur in a mixed content model, or when an element end tag is read.
If positioned on a text-like 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.
The following example displays the text content of each of the elements.
#using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; int main() { XmlNodeReader^ reader = nullptr; try { //Create and load the XML document. XmlDocument^ doc = gcnew XmlDocument; doc->LoadXml( "<book>" "<title>Pride And Prejudice</title>" "<price>19.95</price>" "<misc/>" "</book>" ); //Load the XmlNodeReader reader = gcnew 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 != nullptr ) reader->Close(); } }
Available since 1.1
