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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:10:27+00:00 2026-06-17T08:10:27+00:00

I have been trying the fantabulous CsQuery library, which is basically a .NET port

  • 0

I have been trying the fantabulous CsQuery library, which is basically a .NET port for jQuery allowing the use os CSS selectors and most of jQuery’s functionalities.

I am using it to parse and edit a batch of HTML files (particularly, editing some attributes of different DOM elements).

The following C# snippet shows sort of what I’m doing, with the JavaScript/jQuery equivalent code in the comments.

FileStream doc = File.Open(/*some html file*/);    

CQ dom = CQ.Create(doc);        // $dom = $(document);
CQ images = dom.Select("img");  // $images = $('img');
images.Attr("src","#");         // $images.attr('src','#');

dom.Save(/*Output path*/); // No jQuery equivalent, this just saves the file.

This works perfectly: if I check the output file, all the image’s src values are now #.

Anyway, if I use the Each block (which seems to work nicely using C# lambda expressions to simulate javascript’s function passing) the changes won’t apply to the output file:

FileStream doc = File.Open(/*same html file*/);

CQ dom = CQ.Create(doc);                  // $dom = $(document);
CQ images = dom.Select("img");            // $images = $('img');
images.Each( (element) => {               // $images.each( function(){
  CQ cqElement = CQ.Create(element);      //   $element = $(this);
  cqElement.Attr("src","#");              //   $element.attr('src','#');
  Messagebox.Show(cqElement.Attr("src")); //   alert($element.attr('src'));
});                                       // });

dom.Save(/*Output path*/); // No jQuery equivalent, this just saves the file.

Despite the Messabox showing “#” for every image in my DOM (which means that the cqElement gets the change), the output file doesn’t get the changes.

I think the key line causing the problem is CQ cqElement = CQ.Create(element); since it creates a brand new CsQuery object. In fact, if just after the first Messagebox I pop up another one like the following one, it will not have the changes made to the cqElement

Messagebox.Show(dom.Html()); // alert($dom.html());

Any idea on how could I solve this issue?

  • 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-17T08:10:28+00:00Added an answer on June 17, 2026 at 8:10 am

    You’re absolutely right: CQ cqElement = CQ.Create(element) is the problem. This is at the core of the most siginficant difference between CsQuery and jQuery. With jQuery, there’s only one DOM. With CsQuery, there’s no browser, so there can be any number of distinct DOMs. Each time you use Create it’s a brand new DOM; whereas jQuery methods/selectors return a new CQ object bound to the same DOM.

    The correct way to “wrap” a DOM element as a CQ object like $(element) is with new and not Create, e.g.

    CQ cqElement = new CQ(element);
    

    There’s also a shortcut that’s a method on IDomObject:

    CQ cqElement = element.Cq();
    

    These keep the element in the same DOM. The Create methods always produce a new DOM, and when created from elements, actually clones them so the original DOM is not affected. (Elements can only belong to a single DOM; if you were to create two different DOMs and add elements from one to another using a method like Append, they would be removed from the source. Create automatically clones them.).

    I am sure this could be made more clear in the documentation 🙂

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

Sidebar

Related Questions

I have been trying to use the gem 'character-encodings' which doesn't build in 1.9.2
I have been trying to create a ListView which I can sort using drag
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I have been trying to get autocomplete to work using jquery. jsFiddle of example
I have been trying to learn how to write faster more efficient jQuery and
I have been trying to use fflush to make a progress bar. To test
I have been trying to create a JFormattedTextField array which populates in a pane.
Have been trying on these codes and I want to send a email which
Have been trying to write an awk script which processes a log file, but
I have been trying to use FactoryGirl while developing a Rails 3 mountable engine,

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.