title: Deleting child nodes in treeView
body: <p>There's my code:</p>
  
  <pre><code>private void removeToolStripMenuItem_Click(object sender, EventArgs e)
  {
      if (treeView1.SelectedNode != null)
      {
           if (treeView1.SelectedNode.Parent == null) treeView1.SelectedNode.Remove();
           else if (treeView1.SelectedNode.Parent.Nodes.Count == 1) treeView1.SelectedNode.Parent.Remove();
           else treeView1.SelectedNode.Remove();
      }
       XDocument doc = XDocument.Load("test.xml");
       if (treeView1.SelectedNode.Parent != null)
       {
           var xElement = (from q in doc.Elements("dogs").Elements("dog")
                          where q.Attribute("id").Value == treeView1.SelectedNode.Tag.ToString()
                          select q);
              foreach (var a in xElement)
                  a.Remove();
              doc.Save("test.xml");
  }
  </code></pre>
  
  <p>I want to search through my file for <code>id</code> value and if program will find it, it compares it to tag of selected node, and if it finds it, it'll delete this node. And everytime I'm trying to delete any node, error <code>NullReferenceException was unhandled</code> appears.</p>