2010-03-24 12 views
0

Il est possible d'implémenter INotifyCollectionChanged ou une autre interface comme IObservable pour permettre de lier les données filtrées à partir du fichier XML sur ce fichier a changé? Je vois des exemples avec des propriétés ou une collection, mais avec quoi les fichiers sont-ils modifiés?quelque chose comme INotifyCollectionChanged incendies sur le fichier xml a changé

Je ce code pour filtrer et lier les données XML à la liste boîte:

XmlDocument channelsDoc = new XmlDocument(); 
channelsDoc.Load("RssChannels.xml"); 
XmlNodeList channelsList = channelsDoc.GetElementsByTagName("channel"); 
this.RssChannelsListBox.DataContext = channelsList; 

Répondre

2

Essayez d'utiliser un FileSystemWatcher

private static void StartMonitoring() 
    { 
     //Watch the current directory for changes to the file RssChannels.xml 
     var fileSystemWatcher = new FileSystemWatcher(@".\","RssChannels.xml"); 

     //What should happen when the file is changed 
     fileSystemWatcher.Changed += fileSystemWatcher_Changed; 

     //Start watching 
     fileSystemWatcher.EnableRaisingEvents = true; 
    } 

    static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e) 
    { 
     Debug.WriteLine(e.FullPath + " changed"); 
    } 
+0

Je n'utilise pas Ton conseil dans mon projet mais merci pour info sur cette classe, je la teste et ca marche vraiment, super observateur !! – netmajor

1

Vous devrez implémenter INotifyCollectionChanged vous-même, pour surveiller les changements du système de fichiers utiliser la classe FileSystemWatcher dans System.IO

+0

Je vois que FileSystemWatcher est pas trop bonne idée dans mon projet et essayer INotifyCollectionChanged;) Merci! – netmajor

+0

Donc les changements sont dans votre application et non par des programmes externes changeant un fichier commun? – Michael

1

Le XmlDocument soulève déjà NodeChanged événements. Si vous utilisez un XmlDataProvider comme source de liaison, il écoute les événements NodeChanged et actualise les liaisons. Il actualise également les liaisons si vous modifiez sa propriété Document. Combinez cela avec le FileSystemWatcher et vous êtes sur votre chemin.

Un exemple simple:

<Window x:Class="WpfApplication18.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <XmlDataProvider x:Key="Data" XPath="/Data"> 
      <x:XData> 
       <Data xmlns=""> 
        <Channel>foo</Channel> 
        <Channel>bar</Channel> 
        <Channel>baz</Channel> 
        <Channel>bat</Channel> 
       </Data> 
      </x:XData> 
     </XmlDataProvider> 
    </Window.Resources> 
    <StackPanel Margin="50"> 
     <ListBox ItemsSource="{Binding Source={StaticResource Data}, XPath=Channel}" /> 
     <Button Margin="10" 
       Click="ReloadButton_Click">Reload</Button> 
     <Button Margin="10" 
       Click="UpdateButton_Click">Update</Button> 
    </StackPanel> 
</Window> 

Les gestionnaires d'événements:

private void ReloadButton_Click(object sender, RoutedEventArgs e) 
{ 
    XmlDocument d = new XmlDocument(); 
    d.LoadXml(@"<Data xmlns=''><Channel>foobar</Channel><Channel>quux</Channel></Data>"); 
    XmlDataProvider p = Resources["Data"] as XmlDataProvider; 
    p.Document = d; 
} 

private void UpdateButton_Click(object sender, RoutedEventArgs e) 
{ 
    XmlDataProvider p = Resources["Data"] as XmlDataProvider; 
    XmlDocument d = p.Document; 
    foreach (XmlElement elm in d.SelectNodes("/Data/Channel")) 
    { 
     elm.InnerText = "Updated"; 
    } 
}