Iterator not Iterating

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<br/>
Hi all;<br/>
<br/>
Im having trouble with something that is (or should be) fairly straight-forward.<br/>
<br/>
<br/>
I have a recursive method that traverses an XML file, writing out the<br/>
element tag names and values.<br/>
<br/>
This is a snippet from the XML file:<br/>
<br/>
<pre> <root>

<all_companies>

<company_group company_group_ID_attr="1
<company_group_name>Cleaning</company_group_name>
<company_group_ID>1</company_group_ID>
<company company_ID_attr="2
<name>Bloomburg</name>
<company_ID>2</company_ID>
<Address>blah blah blah</Address>
<employee_refs>
<employee_ref>4</employee_ref>
</employee_refs>
</company>
<company company_ID_attr="4
<name>Morris</name>
<company_ID>4</company_ID>
<Address>blah blah blah</Address>
<employee_refs>
<employee_ref>3</employee_ref>
<employee_ref>1</employee_ref>
</employee_refs>
</company>
<company company_ID_attr="7
<name>Ajax</name>
<company_ID>7</company_ID>
<Address>blah blah blah</Address>
<employee_refs>
<employee_ref>4</employee_ref>
<employee_ref>2</employee_ref>
</employee_refs>
</company>
</company_group>

</all_companies>

</root>

[/code]
<br/>
I use the following XPath expression to access the first <company group> tag:<br/>
"root[1]/all_companies[1]/company_group[1]"<br/>
<br/>
On the first call to the recursive method ("swrite_for_select_certain_stuff"),<br/>
I see with the debugger that I reach the <company_group_name> tag. A recursive<br/>
call at that point takes me to the text node within the tag ("Cleaning"),<br/>
but it is a text type node (not an element), so it returns without writing<br/>
anything to the output stream.<br/>
<br/>
On return from that second call, things go wrong. The Iterator within the<br/>
while loop returns false for the MoveNext method, when it should have moved to<br/>
the <company_group_ID> tag (or so I think it should).<br/>
<br/>
Am I missing something here?<br/>
<br/>
<br/>
OS is Win XP, and .NET is version 4.0.<br/>
<br/>
<br/>
My code is as follows:<br/>
<br/>
<pre> //==================================
//==================================

private void swrite_name_and_value
(System.Xml.XPath.XPathNavigator p_XP_nev)
{
swriter.WriteLine
(this.Indent.str
+ p_XP_nev.Name
+ " >" + p_XP_nev.Value
+ "< [ " + p_XP_nev.NodeType.ToString ()
+ "]");
}
//==================================
//==================================

private void swrite_for_select_certain_stuff (XPathExpression l_query)
{
string new_path;
XPathExpression l2_query;
XPathNavigator CO_Nav;
XPathNavigator CO2_Nav;
XPathNodeIterator CO_Itr;

CO_Itr = this.XP_doc_navigator.Select (l_query);

while (CO_Itr.MoveNext ())
{
CO_Nav = CO_Itr.Current.Clone ();

this.swrite_name_and_value (CO_Nav);

if (CO_Nav.HasChildren)
{
CO2_Nav = CO_Nav.Clone ();
CO2_Nav.MoveToFirstChild ();

if (CO2_Nav.NodeType == XPathNodeType.Element)
{
new_path = l_query.Expression + "/"
+ CO2_Nav.Name;
l2_query = this.XP_doc_navigator.Compile (new_path);
//new_path = CO_Nav.Name;
//l2_query = CO_Nav.Compile (new_path);
//l2_query = CO2_Nav.Compile (new_path);
swrite_for_select_certain_stuff (l2_query);
}

CO2_Nav.MoveToParent ();
}

swriter.WriteLine();
swriter.WriteLine(" ------------------------------");
}
}
//==================================
//==================================

public void select_stuff_01 ()
{
XPathExpression l_query;

swriter = get_stream_writer (FILE_NAME_05);

l_query = this.XP_doc_navigator.Compile
("root[1]/all_companies[1]/company_group[1]");

this.swrite_for_select_certain_stuff (l_query);

swriter.WriteLine(" x----------------------------x");
}
//==================================
//==================================

[/code]
<br/>
<br/>
THANKS!!!<br/><hr class="sig Wally

View the full article
 
Back
Top