title: System.Xml.Linq.XDocument.Save() causes a full harddrive
body: <p>I got a problem and need help. I need to save a XML file, about 400MB. I'm using <code>System.Xml.Linq.XDocument.Save()</code> for this. Normally it is no problem, but sometimes i got a strange behavoir, <code>System.Xml.Linq.XDocument.Save()</code> did save the XML-Data in an endless loop in the same XML, till the harddrive is fuil. Taking the same XML-Data once again, and there is no problem, everything works fine. So it wasn't possilbe to reproduce this behavior, till now!</p>
  
  <p>I got a new server, and now this is the only result i'm getting, everytime System.Xml.Linq.XDocument.Save() is called! It is absolutly reproducable...here is the new environement     </p>
  
  <ul>
  <li>.Net Framework 4 (also 4.5)</li>
  <li>Windows Server 2008 R2 Standard SP1</li>
  <li>32 GB Ram</li>
  <li>64 Bit</li>
  <li>all updates are installed</li>
  </ul>
  
  <p>Thanks for help!</p>
  
  <hr>
  
  <p>Here is the code snippet: </p>
  
  <pre><code>xmlImp = new XDocument(
      new XDeclaration("1.0", null, null),
      new XDocumentType("Hotelstamm", null, "hotel.dtd", null),
      HotelStamm);
  
  string pathUTF8 = path.Replace(".xml", "-utf8.xml");
  try 
  { 
      xmlImp.Save(pathUTF8);
  } 
  catch (IOException) 
  {
  } 
  </code></pre>
  
  <p>Edit:</p>
  
  <p>The Problem was that we created the Xml-Structure in multiple threads and the .Add-Method of XElement is not thread-safe. If it is called in multiple threads it damages the Xml-Stucture and the Xml starts to get infinite. </p>
  
  <p>The Problem was that the application runs fine in about 95% on our old server. But on the new one it had only a 5% chance to succed.<br>
  On the old server  a sinpmle loop with 3 tries made it work. But now with locking it works fine.</p>