• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

xml manipulation, xml nodes iterator

consider a loop like one shown below. If i delete a child node, if some condition is met. It doesn't loop through remaining nodes. i.e the iterator resets. what is a good way to do this?

foreach ( XmlNode node in nodelist)
{
         childList = node.ChildNodes;

         foreach (XmlNode childNode in childList)
                if ( true)                                                   //some condition
                        node.RemoveChild(childNode);
}
0
singsunn
Asked:
singsunn
1 Solution
 
WikkardCommented:
Use a for loop instead of foreach.

From MSDN:
The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now