Q:

Reload xml file if change

I have an XML file and I save some of my application settings in that. 
I read this xml file and cache it in memory, If i change the setting i want to read the latest change from xml file but how can I know that, xml file is changed ?
Im running this app in the web context (asp.net)

A:

You can use a FileSystemWatcher class to monitor the file.
Alternately, you may want to consider savings settings in the web.config file, either in appSettings or a custom configuration section. Your web application will reload automatically when your configuration changes.

A:

The 'FileSystemWatcher' option has been answered, but you could also use the ASP.NET Cache with a 'file depenency'.
I'm assuming you don't want real-time updates being sent to the browser, you just want to use the new values server-side when some other code is running.  
At app startup your app reads the file and inserts the settings object into the ASP.NET cache with a 'file dependency'.  Each time through your code checks to see if the object is in the cache and if found it keeps using the same object.  When the file changes, you guessed it, the cache will evict the old copy of your settings, which means you'll get null back from the cache.  At this point your 'startup' code path runs again, reloads the file, and inserts the new setting object back into the cache (with the dependency).
The ASP.NET Cache is threadsafe, and as long as your deserialization doesn't cause any side-effects it is safe to read and insert without any locking or synchronization.  (This means it is possible for two requests/threads to re-read the updated settings file at the same time, but this is usually OK, as long as the file is small.)  IIRC the FileSystemWatcher solution is harder to get right and requires synchronization.
http://www.dotnetfunda.com/articles/article791-data-caching-with-file-dependency-in-aspnet-.aspx

