<分区>
我需要能够在我的 C# 应用程序中简单地指定来自 html 的元素。我只会使用 Linq to Sql,但这需要可配置/可序列化为字符串。我当然可以使用 XPath,但此时像 Sizzle 这样的东西对大多数人来说更自然。
有人知道 .Net 中是否存在 sizzle 选择器实现吗?
<分区>
我需要能够在我的 C# 应用程序中简单地指定来自 html 的元素。我只会使用 Linq to Sql,但这需要可配置/可序列化为字符串。我当然可以使用 XPath,但此时像 Sizzle 这样的东西对大多数人来说更自然。
有人知道 .Net 中是否存在 sizzle 选择器实现吗?
最佳答案
是的,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");
关于.net - C# 中是否有 Sizzle/jQuery 选择器实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7379832/