You should have started by perusing the official documentation for the two functions:
ReadString()
:
reads the contents of an element or text node as a string. However, we recommend that you use the ReadElementContentAsString
method instead, because it provides a more straightforward way to handle this operation.
ReadContentAsString()
:
Reads the text content at the current position as a String
object.
Whereas ReadString()
works only with two types of node—text and element—ReadContentAsString()
handles a whole bunch of them, including CDATA, whitespace, and comment, but not element! I consider the latter method rather confusing overgeneralised, with its rules for the processing of different node types and the need for the read to be inside an element rather than at the start of it.
I consider ReadString()
the more convenient method because one need only position the reader at the start of an element in order to read its contents. The recommendation to use ReadElementContentAsString()
is in my opinion unjustified, because the recommended method:
reads the start tag, the contents of the element, and moves the reader past the end element tag.
As you see, it is asymmetrical and automatically skips the closing tag, putting the reader at the node that follows. This behavior may easily break certain simple patterns, such as scanning with selective reading. Consider the following code that scans a sequence of "cell" elements in a XML file and reads the contents of only those that have a certain attribute set to the value ReadMe!
string attrib;
string content;
while( NextCell( reader, out attrib ) )
{ if( attrib != "ReadMe!" ) continue;
ReadCell( reader, out content );
}
In my case, NextCell()
leaves the reader at the beginning of a cell element or returns false. ReadCell()
always leaves the reader at the closing tag of the cell element last read. It uses ReadString()
to do that. If it employed ReadElementContentAsString()
the reader would be left at the start of the next cell and NextCell()
would jump over it to the cell after it.