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 4080226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:54:11+00:00 2026-05-20T17:54:11+00:00

I really need to be somewhere else this morning. So, I have decided to

  • 0

I really need to be somewhere else this morning. So, I have decided to post a performance question here instead.

The code below works but it calls Load and Save method multiple times. This seems far from efficient. Please could someone provide the code so far the load and save lines occur outside the loop. I wish to call load and save only once.

Thanks chaps 🙂

 public void RemoveNodes(IList<String> removeItems)

    {

        foreach (String removeItem in removeItems)

        {

            XDocument document = XDocument.Load(fullFilePath);

            var results = from item in document.Descendants(elementName)

                          let attr = item.Attribute(attributeName)

                          where attr != null && attr.Value == removeItem.ToString()

                          select item;

            results.ToList().ForEach(item => item.Remove());

            document.Save(fullFilePath);

        }

    }
  • 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-05-20T17:54:12+00:00Added an answer on May 20, 2026 at 5:54 pm

    You’ve already given the answer yourself – just move the Load and Save calls outside the loop. It’s not clear to me where you were having problems implementing that yourself…

    You can make your query slightly simpler too though:

    XDocument document = XDocument.Load(fullFilePath);
    foreach (String removeItem in removeItems)
    {
        var results = from item in document.Descendants(elementName)
                      where (string) item.Attribute(attributeName) == removeItem
                      select item;
        results.ToList().ForEach(item => item.Remove());
    }
    document.Save(fullFilePath);
    

    This uses the fact that the conversion from XAttribute to string returns null if the attribute reference itself is null.

    You don’t even need to use a query expression:

    var results = document.Descendants(elementName)
              .Where(item => (string) item.Attribute(attributeName) == removeItem);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need some quick tips here. I've got this VBScript script which sends
I really need an answer to this question. I am working on a project
I have an NSXMLElement that is a copy from somewhere else in my code.
Really need just some guidance : Topological sort by arcs definition (from my question)
I really need help on this one. I am having a simple login form
I really need advice on how to do the following. I have tried several
I really need your help for this. I am relatively new to programming and
This is going to be a really dumb question, I just know it, but
There are numerous discussions here that have addressed issues that seem related to this
i am really stuck with this task - i need to populate a report

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.