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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:59:26+00:00 2026-06-13T08:59:26+00:00

This code works: var element = XElement.Parse(<div><span><em>Content</em></span><span><em>Content2</em></span></div>); var spans = element.Descendants(span).ToList(); foreach(var span in

  • 0

This code works:

var element = XElement.Parse("<div><span><em>Content</em></span><span><em>Content2</em></span></div>");

var spans = element.Descendants("span").ToList();

foreach(var span in spans)
{
    span.ReplaceWith(span.Nodes());
}

This does not, giving the error ‘Object reference not set to an instance of an object.’:

var element = XElement.Parse("<div><span><em>Content</em></span><span><em>Content2</em></span></div>");

var spans = element.Descendants("span");

foreach(var span in spans)
{
    span.ReplaceWith(span.Nodes());
}

The only difference is I have removed the ‘ToList()’ when creating the list of Descendant Nodes. Why is this?

IQueryable implements IEnumerable, and I thought iterating forced deferred execution, so why is ‘ToList()’ making a difference here?

  • 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-13T08:59:27+00:00Added an answer on June 13, 2026 at 8:59 am

    In the second case, you’re changing the collection spans while you’re iterating over it, so bad things happen. Maybe it’s trying to give you what it remembers to be the next span, but you’ve already removed it.

    (In core collections like lists, you get an exception explicitly stating that the collection has been modified. For example, in a List<.>, there’s a version number that changes every time the list is modified; when you call ToNext on a list enumerator, if the version has changed, an exception is thrown.

    In LINQ-to-XML it appears that the developers didn’t add this kind of behaviour.)

    In the first case, the list created by ToList isn’t modified when you modify the original tree.

    I’d try something like this:

    XElement span = null; // or whatever class this should be
    while ((span = element.Descendants("span").FirstOrDefault()) != null)
        span.ReplaceWith(span.Nodes());
    

    i.e. replace the spans one by one, rather than trying to get them all in one go and then replace them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this toy code, works fine, using MySQL var r = new SimpleRepository(DB,
This code works but the element with class genbutton-delete needs to be clicked twice
I use this code for generating mouse click in Web browser: var element =
Why doesn't this code work in IE6 or IE7? $('a').click( function() { var urlIsExternal
Hi all Why this piece of javascript code doesn't work on firefox var nfiles
I work on code something like this ... HEADERS ... int *var; void child()
This code works well: UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [pickerView addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
This code works fine in Chrome. However, in Firefox, when it hits the GMxhr
This code works for me except it only checks for a duplicate once, and
This code works well for displaying separate xml items in my main.xml. But is

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.