My question is simple. I've been using ReadStartElement
and ReadEndElement
with the XmlReader
in my code just fine. The question is (and this is after looking at MSDN), do you need to match the two?
In other words, do I end with ReadEndElement
for each and every ReadStartElement
or is are there cases where you don't need so many ReadEndElement
calls in your code when reading an XML Envelop? Thee have been cases where I did not always have a matching ReadEndElement
and reading the xml worked fine.
Yes, I believe you do need to match them most often. This is because both the ReadStartElement
and ReadEndElement
move the XmlReader to the next node. However, they check for different things.
This unnecessarily added verbosity in the code is just one reason I always prefer to navigate XML using an XPathNavigator
. It's convenience is simply unmatched (unless you compare it with LINQ.)