title: Replacing elements in XElement breaks foreach loop
body: <p>I have string </p>
  
  <pre><code>&lt;div class="TextP"&gt;
     &lt;span class="bold" style="font-weight: bold;"&gt;bold&lt;/span&gt; text 
     &lt;span class="bold" style="font-weight: bold;"&gt;italic&lt;/span&gt; text 
     &lt;span class="bold" style="font-weight: bold;"&gt;underlined&lt;/span&gt; text
  &lt;/div&gt;
  </code></pre>
  
  <p>which I parse to <code>XElement</code> object and than I need to replace the formating spans with other elements. So I have written this code</p>
  
  <pre><code>//el is the root div
  foreach (XElement el in e.Elements())
          {
              switch (el.Name.ToString().ToLower())
              {
                  //The method is more complex, but only this part doesnt work, therfore this only case
                  case "span":
                      if (el.Attribute("class") != null)
                      {
                          switch (el.Attribute("class").Value)
                          {
                              case "underline" :
                                  el.ReplaceWith(XElement.Parse("&lt;U&gt;" + el.Value + "&lt;/U&gt;"));
                                  break;
                              case "bold":
                                  el.ReplaceWith(XElement.Parse("&lt;B&gt;" + el.Value + "&lt;/B&gt;"));
                                  break;
                              case "italic":
                                  el.ReplaceWith(XElement.Parse("&lt;I&gt;" + el.Value + "&lt;/I&gt;"));
                                  break;
                          }
                      }
                      break;
              }
          }
  </code></pre>
  
  <p>The problem is that when I replace the first <code>span</code> the foreach loop breaks and the two other <code>spans</code> remain unreplaced.
  I think It's because the <code>.Elements()</code> collection changes, but I can't  figure out, how should I change my code.</p>