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

  • SEARCH
  • Home
  • 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 8095653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:12:19+00:00 2026-06-05T21:12:19+00:00

This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>

  • 0

This one is a little tricky. Say I have this XmlDocument

<Object>
    <Property1>1</Property1>
    <Property2>2</Property2>
    <SubObject>
         <DeeplyNestedObject />
    </SubObject>
</Object>

I want to get back this

<Object>
    <Property1>1</Property1>
    <Property2>2</Property2>
</Object>

Since each of the children of <SubObject> are all empty elements I want to get rid of it. What makes it challenging is that you cant remove nodes as you’re iterating over them. Any help would be much appreciated.

UPDATE Here’s what I wound up with.

public XDocument Process()
{
    //Load my XDocument
    var xmlDoc = GetObjectXml(_source);

    //Keep track of empty elements
    var childrenToDelete = new List<XElement>();

    //Recursively iterate through each child node
    foreach (var node in xmlDoc.Root.Elements())
        Process(node, childrenToDelete);

    //An items marked for deletion can safely be removed here
    //Since we're not iterating over the source elements collection
    foreach (var deletion in childrenToDelete)
        deletion.Remove();

    return xmlDoc;
}

private void Process(XElement node, List<XElement> elementsToDelete)
{
    //Walk the child elements
    if (node.HasElements)
    {
        //This is the collection of child elements to be deleted 
        //for this particular node
        var childrenToDelete = new List<XElement>();

        //Recursively iterate each child
        foreach (var child in node.Elements())
            Process(child, childrenToDelete);

        //Delete all children that were marked as empty
        foreach (var deletion in childrenToDelete)
            deletion.Remove();

        //Since we just removed all this nodes empty children
        //delete it if there's nothing left
        if (node.IsEmpty)
            elementsToDelete.Add(node);
    }

    //The current leaf node is empty so mark it for deletion
    else if (node.IsEmpty)
        elementsToDelete.Add(node);
}

If anyone is interested in the use case for this it’s for an ObjectFilter project I put together.

  • 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-05T21:12:22+00:00Added an answer on June 5, 2026 at 9:12 pm

    It’ll be rather slow, but you could do this:

    XElement xml;
    while (true) {
        var empties = xml.Descendants().Where(x => x.IsEmpty && !x.HasAttributes).ToList();
        if (empties.Count == 0)
            break;
    
        empties.ForEach(e => e.Remove());
    }
    

    To make it faster, you could walk up the parent nodes after the first iteration and see if they’re empty.

    XElement xml;
    var empties = xml.Descendants().Where(x => x.IsEmpty && !x.HasAttributes).ToList();
    while (empties.Count > 0) {
        var parents = empties.Select(e => e.Parent)
                             .Where(e => e != null)
                             .Distinct()    //In case we have two empty siblings, don't try to remove the parent twice
                             .ToList();
    
        empties.ForEach(e => e.Remove());
    
        //Filter the parent nodes to the ones that just became empty.
        parents.RemoveAll(e => e.IsEmpty && !e.HasAttributes);
        empties = parents;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has proven to be a little tricky for me so far. I
So, this one seems to be a little tricky. There's an image map with
in MySql database I have to make some little tricky query. I must get
Tricky one to explain this. I have a custom built properties grid. The left
I'm afraid that this one is a little tricky since I couldn't recreate the
This one is a little tricky to explain. I would like to create a
This is a little tricky - I have a db with tables encoded as
This one is a little confusing to me. I'm running a LAMP server with
This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes
This one is a huge issue: first off, while I know a little bit

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.