4

I noticed in some open source code, the usage of System.Xml.XmlReader.ReadString() to parse Xml. But when I tried to use it in a console app just to check it out (I'm curious girl by nature) I didn't get any intellisense when I press . against an XmlReader class instance variable.

I got really intrigued and inspected the MSIL using ILDASM. That's when I noticed that this class method is decorated with

[EditorBrowsable(EditorBrowsableState.Never)]

Also noticed that the call I made to ReadString() has been converted to another method that goes by the name of ReadContentAsString. I am able to find zero documentation on MSDN on why the Microsoft programmer made this decision.

Anyone? I really want to know so that I am calling the right method in my production code.

Also don't want any future developers who see my code dismiss me as a dummy because I am a girl and a developer (rare combination from what I see in a highly male dominated profession ;)

Barb C. Goldstein
  • 454
  • 1
  • 3
  • 13
  • 1
    [This question](http://stackoverflow.com/questions/21174168/is-xmlreader-readstring-deprecated-or-obsoleted) appears to ask the same question but includes reasoning why. – M.Babcock Jan 18 '14 at 03:36
  • Could you maybe explain what it is you are trying to do (i.e. what prompted the question)? – theMayer Jan 18 '14 at 04:16

1 Answers1

0

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 elementReadContentAsString() 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.

Anton Shepelev
  • 922
  • 9
  • 19