In my code, I want to remove the img tag which doesn\'t have src value. I am using HTMLAgilitypack\'s HtmlDocument object. I am finding the img whic
It seems you're modifying the collection during the enumeration by using HtmlNode.RemoveChild method.
To fix this you need is to copy your nodes to a separate list/array by calling e.g. Enumerable.ToList
var nodesToRemove = doc.DocumentNode
.SelectNodes("//img[not(string-length(normalize-space(@src)))]")
.ToList();
foreach (var node in nodesToRemove)
node.Remove();
If I'm right, the problem will disappear.