Search code examples
c#c#-2.0xpathnodeiterator

synchronization qustion on move XPathNodeIterator and Child XPathNodeIterator


I created two XPathNodeIterator it and childIt in my code

Code snippet as this,

string selectSfStr = "Equipment/Main/Sub";
            it = nav.Select(selectSfStr);

            while (it.MoveNext())
            {                
                ; // do something here

                if (it.Current.HasChildren)
                {


                    XPathNodeIterator childIt;
                    string selectChildSfStr = "//item";
                    childIt = nav.Select(selectChildSfStr);

                    while (childIt.MoveNext())
                    {
                           ; // do something here, but I found bug. The childIt can't move sychronized with the parent `it`.
                           ;// How can I synchronize `childIt` here when I moved to next `it`.
                    }
                 }
         }

my xml file nested with the sequence of Equipment/Main/Sub/item and there are multi sub nodes and multi item for each of sub nodes


Solution

  • Eventually, I fixed this bug as this,

    while (it.MoveNext())
                {                
                    // do something here
    
    
                    if (it.Current.HasChildren)
                    {
    
    
                        XPathNodeIterator childIt;
                        childIt = null;                    
                        childIt = it.Current.SelectChildren("item", ""); 
    
                        while (childIt.MoveNext())
                        {
                           // do something here
    
                           childIt.Current.MoveToParent();
                        }
                    }
              }