title: WPF two-way binding XML
body: <p>I'm struggling to get to grips with WPF, more specifically performing two way binding of an xml file. Should I be using XMLDataProvider or is their another(better) option?
  The data is displaying fine but when I change an entry the changes aren't reflected in the xml file.</p>
  
  <p>The XML:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Licence&gt;
   &lt;Market&gt;
    &lt;Name&gt;DAX&lt;/Name&gt;
    &lt;Begin&gt;01/01/2010&lt;/Begin&gt;
    &lt;End&gt;01/04/2010&lt;/End&gt;
   &lt;/Market&gt;
   &lt;Market&gt;
    &lt;Name&gt;DJI&lt;/Name&gt;
    &lt;Begin&gt;01/07/2010&lt;/Begin&gt;
    &lt;End&gt;01/10/2010&lt;/End&gt;
   &lt;/Market&gt;
  &lt;/Licence&gt;
  </code></pre>
  
  <p>The XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.Resources&gt;
      &lt;DataTemplate x:Key="LicenceTemplate"&gt; 
          &lt;Label Content="{Binding XPath=Name}"/&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;Grid.DataContext&gt;
          &lt;XmlDataProvider x:Name="XMLData" Source="XMLFile1.xml" XPath="Licence/Market"/&gt;
  
      &lt;/Grid.DataContext&gt;
      &lt;StackPanel&gt;
          &lt;DataGrid x:Name="DataGridLic"  ItemsSource="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="300" CellEditEnding="DataGridLic_CellEditEnding"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn x:Name="nameColumn" Binding="{Binding XPath=Name, Mode=TwoWay}" Header="Name" Width="100" Foreground="#FFC28383" /&gt;
                  &lt;DataGridTextColumn x:Name="BegColumn" Binding="{Binding XPath=Begin, Mode=TwoWay}" Header="Begin" Width="100" Foreground="#FFC14040" /&gt;
                  &lt;DataGridTextColumn x:Name="EndColumn" Binding="{Binding XPath=End, Mode=TwoWay}" Header="End" Width="100" Foreground="#FFC14040" /&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
  
  
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>The CodeBehind:</p>
  
  <pre><code> public MainWindow()
      {
          InitializeComponent();
      }
  
      private void DataGridLic_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
      {
         XMLData.Document.Save("XMLFile1.xml");
      }
  </code></pre>
  
  <p>UPDATE:
  Some useful resources for xml databinding:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/bb669141.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/bb669141.aspx</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/cc165615.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/cc165615.aspx</a></p>