title: Recursively remove xml nodes using Linq to XML
body: <p>Fairly new to Linq to XML How does one remove <strong>xml node</strong>( recursively using relation ) and <strong>save</strong> the document.</p>
  
  <p>Structure of the xml cannot be altered as it comes from service.Below is the xml from a tasks service. Every task can have nested tasks for which there might be one or more nested tasks. Nesting is intended to be upto <code>N level</code>.</p>
  
  <ul>
  <li><p>When one of parent tasks are removed using linq to xml how do i remove all of it's children?</p></li>
  <li><p>How do i know all the nodes where successfully removed?</p></li>
  </ul>
  
  <p>Xml:</p>
  
  <pre><code>&lt;Tasks&gt;
    &lt;Task ID="1"&gt;Clean the Room&lt;/Task&gt;
    &lt;Task ID="2"&gt;Clean the Closet&lt;/Task&gt;
    &lt;Task ID="3" ParentId="2"&gt;Remove the toys&lt;/Task&gt;
    &lt;Task ID="4" ParentId="3"&gt;Stack action Figures on Rack&lt;/Task&gt;
    &lt;Task ID="5" ParentId="3"&gt;Put soft toys under bed&lt;/Task&gt;
  &lt;/Tasks&gt;
  </code></pre>
  
  <p>In above xml when <code>taskId=2</code> is removed, it's sub-tasks namely <code>Id = 3</code> should be removed. Since <code>3</code> is removed it's subtasks <code>4</code> and <code>5</code> should be removed as well.</p>