Jan Obrestad wrote:
Quote:
string xml =
"<main><textline><textelement>A</textelement><textelement>B</textelement><textelement>C</textelement></textline></main>";
>
StringReader strReader = new StringReader(xml);
XmlReader reader = XmlReader.Create(strReader);
reader.MoveToContent();
|
Now the reader is positioned on the 'main' start tag.
Quote:
int i = 0;
while (!(reader.NodeType == XmlNodeType.EndElement &&
reader.Name.Equals("textline")))
{
reader.Read();
if (reader.NodeType == XmlNodeType.Element &&
reader.Name.Equals("textelement"))
{
string text = reader.ReadElementString();
Console.WriteLine("{0} {1}", i, text);
i++;
}
}
|
For the first run of the loop:
reader.Read() moves reader to the 'textline' start tag.
On the second run of the loop:
reader.Read() moves reader to the 'textelement' start tag.
reader.ReadElementString() moves reader past the 'textelement' end
tag, meaning it is now positioned on the second 'textelement' start tag.
Output: i as 0, text as 'A'.
i set to 1
On the third run of the loop:
reader.Read() moves reader to the text node with contents 'B'.
So there you have the problem, your combination of
Read/ReadElementString and your conditions fail to find the second
'textelement' start tag.
As a solution you might want to use ReadString() instead of
ReadElementString().
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/