At the following code i use foreach loop to check each node in nodelist and remove some of them. after i remove one node the foreach loop throw the following error: \"The el
As you're not simply removing items from the collection you're looping over, I'm not sure if "use a for loop" will work.
The following takes two steps:
public static XmlNodeList Scan(XmlNodeList nodeList)
{
List toRemove = new List();
foreach (XmlNode xmlElement in nodeList)
{
string elementValue = xmlElement.InnerText;
if (elementValue.Length < 6 || elementValue.Substring(0, 3) != "999")
{
toRemove.Add(xmlElement);
}
}
foreach(XmlNode xmlElement in toRemove)
{
XmlNode node = xmlElement.ParentNode;
node.RemoveChild(xmlElement);
}
return nodeList;
}