是否有可以在 C# 中使用的类似 jQuery 的 CSS/HTML 选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1580635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:01:25  来源:igfitidea点击:

Is there a jQuery-like CSS/HTML selector that can be used in C#?

c#jquerycssjquery-selectorscss-selectors

提问by Dave

I'm wondering if there's a jQuery-like css selector that can be used in C#.

我想知道是否有可以在 C# 中使用的类似 jQuery 的 css 选择器。

Currently, I'm parsing some html strings using regex and thought it would be much nicer to have something like the css selector in jQuery to match my desired elements.

目前,我正在使用正则表达式解析一些 html 字符串,并认为在 jQuery 中使用 css 选择器之类的东西来匹配我想要的元素会更好。

采纳答案by Andy S

You should definitely see @jamietre's CsQuery. Check out his answer to this question!

您绝对应该看到@jamietre 的CsQuery。看看他对这个问题的回答!

Fizzlerand Sharp-Queryprovide similar functionality, but the projects seem to be abandoned.

FizzlerSharp-Query提供了类似的功能,但这些项目似乎被放弃了。

回答by Frank Schwieterman

For XML you might use XPath...

对于 XML,您可能会使用 XPath ...

回答by patjbs

I'm not entirely clear as to what you're trying to achieve, but if you have a HTML document that you're trying to extract data from, I'd recommend loading it with a parser, and then it becomes fairly trivial to query the object to pull desired elements.

我并不完全清楚您要实现的目标,但是如果您有一个要从中提取数据的 HTML 文档,我建议您使用解析器加载它,然后就变得相当简单了查询对象以拉取所需的元素。

The parser I linked above allows for use of XPath queries, which sounds like what you are looking for.

我上面链接的解析器允许使用 XPath 查询,这听起来像您正在寻找的。

Let me know if I've misunderstood.

如果我误解了,请告诉我。

回答by Daniel

Not quite jQuery like, but this may help: http://www.codeplex.com/htmlagilitypack

不太像 jQuery,但这可能会有所帮助:http: //www.codeplex.com/htmlagilitypack

回答by Jamie Treworgy

Update 10/18/2012

2012 年 10 月 18 日更新

CsQueryis now in release 1.3. The latest release incorporates a C# port of the validator.nuHTML5 parser. As a result CsQuery will now produce a DOM that uses the HTML5 spec for invalid markup handling and is completely standards compliant.

CsQuery现在是 1.3 版。最新版本包含了validator.nuHTML5 解析器的 C# 端口。因此,CsQuery 现在将生成一个 DOM,该 DOM 使用 HTML5 规范进行无效标记处理,并且完全符合标准。

Original Answer

原答案

Old question but new answer. I've recently released version 1.1 of CsQuery, a jQuery port for .NET 4 written in C# that I've been working on for about a year. Also on NuGetas "CsQuery"

老问题,但新答案。我最近发布了 CsQuery 的 1.1 版,这是一个用 C# 编写的用于 .NET 4 的 jQuery 端口,我已经研究了大约一年。同样在NuGet 上作为“CsQuery”

The current release implements all CSS2 & CSS3 selectors, all jQuery extensions, and all jQuery DOM manipulation methods. It's got extensive test coverage including all the tests from jQuery and sizzle (the jQuery CSS selection engine). I've also included some performance tests for direct comparisons with Fizzler; for the most part CsQuery dramatically outperforms it. The exception is actually loading the HTML in the first place where Fizzler is faster; I assume this is because fizzler doesn't build an index. You get that time back after your first selection, though.

当前版本实现了所有 CSS2 和 CSS3 选择器、所有 jQuery 扩展和所有 jQuery DOM 操作方法。它具有广泛的测试覆盖范围,包括来自 jQuery 和 sizzle(jQuery CSS 选择引擎)的所有测试。我还包含了一些性能测试,用于与 Fizzler 进行直接比较;在大多数情况下,CsQuery 显着优于它。例外实际上是首先在 Fizzler 更快的地方加载 HTML;我认为这是因为 fizzler 没有建立索引。不过,您可以在第一次选择后恢复这段时间。

There's documentation on the github site, but at a basic level it works like this:

github 站点上有文档,但在基本级别上它的工作方式如下:

Create from a string of HTML

从一串 HTML 创建

CQ dom = CQ.Create(htmlString);

Load synchronously from the web

从网络同步加载

CQ dom = CQ.CreateFromUrl("http://www.jquery.com");

Load asynchronously (non-blocking)

异步加载(非阻塞)

CQ.CreateFromUrlAsync("http://www.jquery.com", responseSuccess => {
    Dom = response.Dom;        
}, responseFail => {
    ..
});

Run selectors & do jQuery stuff

运行选择器并做 jQuery 的事情

var childSpans = dom["div > span"];
childSpans.AddClass("myclass");

the CQobject is like thejQuery object. The property indexer used above is the default method (like $(...).

CQ对象就像 jQuery 对象。上面使用的属性索引器是默认方法(如$(...).

Output:

输出:

string html = dom.Render();