It seems every time I use an XMLReader, I end up with a bunch of trial and error trying to figure out what I'm about to read versus what I'm reading versus what I just read. I always figure it out in the end, but I still, after using it numerous times, don't seem to have a firm grasp of what an XMLReader is actually doing when I call the various functions. For example, when I call Read the first time, if it reads an element start tag, is it now at the end of the element tag, or ready to begin reading the element's attributes? Does it know the values of the attributes yet if I call GetAttribute? What will happen if I call ReadStartElement at this point? Will it finish reading the start element, or look for the next one, skipping all the attributes? What if I want to read a number of elements -- what's the best way to try to read the next element and determine what its name is. Will Read followed by IsStartElement work, or will IsStartElement be returning information about the node following the element I just read?
As you can see I really am lacking an understanding of where an XMLReader is at during the various phases of its reading and how its state is affected by various read functions. Is there some simple pattern that I've simply failed to notice?
Here's another example of the problem (taken from the responses):
string input = "<machine code=\"01\">The Terminator" +
"<part code=\"01a\">Right Arm</part>" +
"<part code=\"02\">Left Arm</part>" +
"<part code=\"03\">Big Toe</part>" +
"</machine>";
using (System.IO.StringReader sr = new System.IO.StringReader(input))
{
using (XmlTextReader reader = new XmlTextReader(sr))
{
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.MoveToContent();
while(reader.Read())
{
if (reader.Name.Equals("machine") && (reader.NodeType == XmlNodeType.Element))
{