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?