Let's say I want to loop through the following XML fragment and print
out the "car" values:
<cars007DoesNotDrive>
<car>Chevrolet</car>
<car>Ford</car>
</cars007DoesNotDrive>
I can write this pseudocode fragment:
===========================
string xml = @"<cars007DoesNotDrive>
<car>Chevrolet</car>
<car>Ford</car>
</cars007DoesNotDrive>";
XmlTextReader xmlReader = new XmlTextReader(xml, XmlNodeType.Element, null);
while (xmlReader.Read()
{
xmlReader.MoveToElement();
if (xmlReader.NodeType == XmlNodeType.Element)
{
switch (xmlReader.Name)
{
case "car":
Console.Write(xmlReader.ReadInnerXml());
break;
}
}
}
=========================
This code will work fine given the above XML; that is, it will print out
two values ("Chevrolet", "Ford") to the console. But suppose, instead,
the XML fragment looks this this:
<cars007DoesNotDrive><car>Chevrolet</car><car>Ford</car></cars007DoesNotDrive>
When I remove the nice formatting, the above code suddenly doesn't not
work. It will instead print out just one value: "Chevrolet," and skip
"Ford" altogether -- that is, the node following the one where I did a
ReadInnerXml().
I'd sure appreciate some pointers in how to solve this little, vexing
problem.
--Brent