2013-07-24 70 views
2

在我的解决方案中我有两个项目(Windows服务和WPF来管理服务) ,我有一个坐在文件(XML),服务使用 我问的是:如何监视xml文件(添加 - 编辑 - 删除)节点

我想知道,当用户使用了XML文件中的任何改变重新启动该服务 ,知道什么是XML节点(S)已经改变

我有搜查了很多,发现解决方案是通过FileSystemWatcher

侦听文件系统更改通知,并在 目录或目录中的文件发生更改时引发事件。

,但我怎么知道是什么改变了

感谢

+0

如何读取XML配置?你使用XPath还是你有一些你想要序列化/反序列化为XML的Settings类? –

+0

它是简单的xml文件,我使用Linq到XML – tito11

回答

0

我会保持原始XML的副本,然后运行时,FileSystemWatcher的触发比较XML节点(S)。有关比较XML节点的建议,请参阅Efficient algorithm for comparing XML nodes

文件守望例如

String folderLocation = System.Configuration.ConfigurationManager.AppSettings["FOLDER_LOCATION"].ToString(); 

      _watcher = new System.IO.FileSystemWatcher(); 
      _watcher.Path = folderLocation; 
      _watcher.IncludeSubdirectories = false; 
      _watcher.NotifyFilter = NotifyFilters.Size; 
      _watcher.Changed += new FileSystemEventHandler(OnFileChanged); 
      _watcher.EnableRaisingEvents = true; 

private void OnFileChanged(object sender, FileSystemEventArgs e) 
     { 
      String file = e.FullPath; 
...