You cannot modify an IEnumerable while looping through it.
Since LINQ to XML uses deferred execution, it's trying to find the descendants as you modify the XML.
To fix this, you need to put the elements in an array before looping through them, like this:
var results = myDocument.Descendants("myName").ToArray();
foreach (var result in results){
if (sth...){
result.replace(myXElement);
}
}
By calling ToArray()
, you force it to enumerate through the elements immediately instead of finding them as you loop through them.
SLaks
2010-06-07 22:16:57