title: Dealing with IOExceptions with XmlWriter.Create() and XmlDocument.Load() in separate threads
body: <p>I have inherited some code which involves a scheduled task that writes data (obtained from an external source) to XML files, and a website that reads said XML files to get information to be presented to the visitor.</p>
  
  <p>There is no synchronization in place, and needless to say, sometimes the scheduled task fails to write the file because it is currently open for reading.</p>
  
  <p>The heart of the writer code is:</p>
  
  <pre><code>XmlWriter writer = XmlWriter.Create(fileName);
  try
  {
    xmldata.WriteTo(writer);
  }
  finally
  {
    writer.Close();
  }
  </code></pre>
  
  <p>And the heart of the reader code is:</p>
  
  <pre><code>XmlDocument theDocument = new XmlDocument();
  theDocument.Load(filename);
  </code></pre>
  
  <p>(yep no exception handling at either end)</p>
  
  <p>I'm not sure how to best approach trying to synchronize these. As far as I know neither XmlWriter.Create() nor XmlDocument.Load() take any parameters regarding file access modes. Should I manage the underlying FileStreams myself (with appropriate access modes) and use the .Create() and .Load() overloads that take Stream parameters?</p>
  
  <p>Or should I just catch the IOExceptions and do some sort of "catch, wait a few seconds, retry" approach?</p>