13

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?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 6
    [CsQuery](http://github.com/jamietre/CsQuery) seems to be very promising. I have not used it yet, just read about it -- after browsing through this question. Should not be closed. – robert4 Sep 28 '13 at 01:32

1 Answers1

19

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");
alexn
  • 57,867
  • 14
  • 111
  • 145