XmlTextReader reading behavior

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
My question is this: when using classic methods for reading xml files using XmlTextReader I noticed that after using the Read() method reader sometimes advances and sometimes does not. My inability to figure this out causes problems as different logic is
executed in simingly the same situations.
When I specify, for example, /TestSchema/FirstNode/SecondNode/ThirdNode then, upon executing my reading logic, reader is positioned at </SecondNode>, loops aruond, executes doc.Read(), still stays (!!!) at the </SecondNode> and then goes into
the third swich case (node is an end element).
When I specify a different path/TestSchema/FirstNode/SecondNode then the same thing happens as above but this time doc.Read() actually advances the reader to the next node, which might be another <SecondNode> and thus falls under different switch
case statement.
Here is sample XML
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; TestSchema<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SomeField <span style="color:Red; SomeAttribute<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; SomeAttributeValue<span style="color:Black; " <span style="color:Red; AnotherAttribute<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; AnotherAttributeValue<span style="color:Black; "<span style="color:Blue; >SomeValue<span style="color:Blue; </<span style="color:#A31515; SomeField<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SomeAttributeNode <span style="color:Red; NodeName<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; NodeValue<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; SomeNode <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Value<span style="color:Black; "<span style="color:Blue; ><span style="color:Blue; </<span style="color:#A31515; SomeNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Field<span style="color:Blue; >FieldValue<span style="color:Blue; </<span style="color:#A31515; Field<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; FirstNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SecondNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ThirdNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; FirstElement<span style="color:Blue; >First<span style="color:Blue; </<span style="color:#A31515; FirstElement<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; SecondElement<span style="color:Blue; >2<span style="color:Blue; </<span style="color:#A31515; SecondElement<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ThirdNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ThirdNode<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ThirdElement<span style="color:Blue; >Third<span style="color:Blue; </<span style="color:#A31515; ThirdElement<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; FourthElement<span style="color:Blue; >4<span style="color:Blue; </<span style="color:#A31515; FourthElement<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ThirdNode<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; SecondNode<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; FirstNode<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; TestSchema<span style="color:Blue; >
[/code]
Code template for iteration
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; while(doc.Read())
{
<span style="color:Blue; switch (doc.NodeType)
{
<span style="color:Blue; case XmlNodeType.Element:
<span style="color:Blue; if (doc.IsStartElement())
<span style="color:Green; // my logic here
<span style="color:Blue; break;
<span style="color:Blue; case XmlNodeType.Text:
<span style="color:Green; // my logic here
<span style="color:Blue; break;
<span style="color:Blue; case XmlNodeType.EndElement:
<span style="color:Green; // my logic here
<span style="color:Blue; break;
}
}
[/code]


View the full article
 
Back
Top