title: WPF - adding a node to a treeview and saving it as XML
body: <p>I have the following code to open an XML file and bind it to a TreeView, but how do I add a child node or parent node to the selected node? Thank you.</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;HierarchicalDataTemplate x:Key="NodeTemplate"&gt;
          &lt;HierarchicalDataTemplate.ItemsSource&gt;
              &lt;Binding XPath="child::*" /&gt;
          &lt;/HierarchicalDataTemplate.ItemsSource&gt;
          &lt;TextBlock Text="{Binding Path=Name}" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
      &lt;XmlDataProvider x:Key="xmlDataProvider"&gt;&lt;/XmlDataProvider&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;TreeView Margin="0,24,0,143"
                Name="treeView1"
                Background="AliceBlue"
                ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}"
                ItemTemplate= "{StaticResource NodeTemplate}"/&gt;
  
      &lt;DockPanel Height="59"
                 Name="dockPanel1"
                 VerticalAlignment="Bottom"
                 Background="AliceBlue"&gt;&lt;/DockPanel&gt;
      &lt;DockPanel Height="23"
                 Name="dockPanel2"
                 VerticalAlignment="Top"
                 Background="AliceBlue"&gt;
          &lt;Button Height="23"
                  Name="button1"
                  Width="75"
                  Click="button1_Click"&gt;Open&lt;/Button&gt;
      &lt;/DockPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Button1 event:</p>
  
  <pre><code>            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
              open.Filter = "XML Files (*.xml)|*.xml";
              if (open.ShowDialog(this) == true)
              {
                  XmlDocument x = new XmlDocument();
                  x.Load(open.FileName);
                  XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider;
                  dataProvider.Document = x;
              }
  </code></pre>