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?
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");
精彩评论