Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Sizzle/jQuery selectors implementation in C#? [closed]

I need to be able to simply specify elements from html in my C# application. I would just use Linq to Sql but this needs to be configurable/serializable to a string. I could of course use XPath but something like Sizzle at this point is just so much more natural for most people.

Anyone know if a sizzle selectors implementation exists in .Net?

like image 773
George Mauer Avatar asked Sep 11 '11 17:09

George Mauer


People also ask

What is Sizzle Selector?

Sizzle is a JavaScript selector library that offers powerful ways to select elements. You can select based off text contained (or not contained) within elements, the existence of child elements inside a parent, or if those elements do not exist.

What is sizzle jQuery?

Sizzle. js is a JavaScript library that implements a "CSS selector engine designed to be easily dropped in to a host library." jQuery uses it internally for its CSS selection needs. If you wanted a CSS engine and had no need for all the other JavaScript benefits of jQuery, you could use Sizzle.

How jQuery selectors are executed?

If dealing with more than two selectors in a row then your last selectors are always executed first. For example, jQuery will first find all the elements with class “. list” and then it will select all the elements with the id “second”.

How many types of jQuery selectors are available?

Two selectors: visible and: hidden are also available in JQuery.


1 Answers

Yepp, Fizzler. It's built upon HtmlAgilityPack and works very well, even though the authors says it's beta. We use it in production on a major project. Samples from the documentation:

// Load the document using HTMLAgilityPack as normal
var html = new HtmlDocument();
html.LoadHtml(@"
  <html>
      <head></head>
      <body>
        <div>
          <p class='content'>Fizzler</p>
          <p>CSS Selector Engine</p></div>
      </body>
  </html>");

// Fizzler for HtmlAgilityPack is implemented as the 
// QuerySelectorAll extension method on HtmlNode

var document = htmlDocument.DocumentNode;

// yields: [<p class="content">Fizzler</p>]
document.QuerySelectorAll(".content"); 

// yields: [<p class="content">Fizzler</p>,<p>CSS Selector Engine</p>]
document.QuerySelectorAll("p");

// yields empty sequence
document.QuerySelectorAll("body>p");

// yields [<p class="content">Fizzler</p>,<p>CSS Selector Engine</p>]
document.QuerySelectorAll("body p");

// yields [<p class="content">Fizzler</p>]
document.QuerySelectorAll("p:first-child");
like image 63
alexn Avatar answered Nov 10 '22 02:11

alexn