Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7884357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:46:39+00:00 2026-06-03T04:46:39+00:00

Fairly new to Linq to XML How does one remove xml node ( recursively

  • 0

Fairly new to Linq to XML How does one remove xml node( recursively using relation ) and save the document.

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 N level.

  • When one of parent tasks are removed using linq to xml how do i remove all of it’s children?

  • How do i know all the nodes where successfully removed?

Xml:

<Tasks>
  <Task ID="1">Clean the Room</Task>
  <Task ID="2">Clean the Closet</Task>
  <Task ID="3" ParentId="2">Remove the toys</Task>
  <Task ID="4" ParentId="3">Stack action Figures on Rack</Task>
  <Task ID="5" ParentId="3">Put soft toys under bed</Task>
</Tasks>

In above xml when taskId=2 is removed, it’s sub-tasks namely Id = 3 should be removed. Since 3 is removed it’s subtasks 4 and 5 should be removed as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:46:41+00:00Added an answer on June 3, 2026 at 4:46 am

    I’m going to assume that with the xml:

    <Tasks> 
      <Task ID="1">Clean the Room</Task>
      <Task ID="2">Clean the Closet</Task>
      <Task ID="3" ParentId="2">Remove the toys</Task>
      <Task ID="4" ParentId="3">Stack action Figures on Rack</Task>
      <Task ID="5" ParentId="3">Put soft toys under bed</Task>
    
      <Task note="test node" />
      <Task ID="a" note="test node" />
    </Tasks>
    

    If Task ID=2 is removed, here is one solution:

    // tasks = XDocument.root;
    
    public static void RemoveTasksAndSubTasks(XElement tasks, int id)
    {
      List<string> removeIDs = new List<string>();
      removeIDs.Add(id.ToString());
    
      while (removeIDs.Count() > 0)
      {
        // Find matching Elements to Remove
        // Check for Attribute, 
        // so we don't get Null Refereence Exception checking Value
    
        var matches = 
            tasks.Elements("Task")
                 .Where(x => x.Attribute("ID") != null
                             && removeIDs.Contains(x.Attribute("ID").Value));
    
        matches.Remove();
    
        // Find all elements with ParentID 
        // that matches the ID of the ones removed.
        removeIDs = 
            tasks.Elements("Task")
                 .Where(x => x.Attribute("ParentId") != null
                             && x.Attribute("ID") != null
                             && removeIDs.Contains(x.Attribute("ParentId").Value))
                 .Select(x => x.Attribute("ID").Value)
                 .ToList();
    
      }
    }
    

    Result:

    <Tasks> 
      <Task ID="1">Clean the Room</Task>
    
      <Task note="test node" />
      <Task ID="a" note="test node" />
    </Tasks>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to .NET C# development and recently started using LINQ to
I'm fairly new to Linq and struggling using dynamic where over a many to
I am Fairly new to Linq and I am trying to write a simple
I'm fairly new to LINQ and trying to find a more elegant way (other
I'm fairly new to using AJAX in ASP.NET (although I am very familiar with
I'm fairly new to linq and wanted to delve in a bit deeper by
I'm fairly new to asp.net and especially LINQ and SQL. Say I have a
I am fairly new to Entity framework as well as LINQ. I have used
Ok, I am fairly new to Linq and I have been looking at the
I am fairly new to LINQ. I am looking at this code, and not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.