【问题标题】:Is there a Sizzle/jQuery selectors implementation in C#? [closed]C# 中是否有 Sizzle/jQuery 选择器实现? [关闭]
【发布时间】:2011-11-14 20:12:31
【问题描述】:
我需要能够在我的 C# 应用程序中简单地指定来自 html 的元素。我只会使用 Linq to Sql 但这需要可配置/可序列化为字符串。我当然可以使用 XPath,但此时像 Sizzle 这样的东西对大多数人来说更加自然。
有人知道 .Net 中是否存在嘶嘶声选择器实现吗?
【问题讨论】:
-
CsQuery 似乎很有希望。我还没有使用它,只是阅读了它——在浏览了这个问题之后。不应关闭。
标签:
.net
html
xml
css-selectors
sizzle
【解决方案1】:
是的,Fizzler。它建立在 HtmlAgilityPack 之上并且运行良好,尽管作者说它是测试版。我们在一个主要项目的生产中使用它。文档中的示例:
// 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");