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

C#JqueryCssJquery SelectorsCss Selectors

C# Problem Overview


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

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.

C# Solutions


Solution 1 - C#

Update 10/18/2012

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

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 [NuGet][3] as "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.

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

Create from a string of 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

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

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

Output:

string html = dom.Render();

[2]: https://github.com/jamietre/CsQuery "CsQuery" [3]: http://www.nuget.org/packages/CsQuery

Solution 2 - C#

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

Fizzler and Sharp-Query provide similar functionality, but the projects seem to be abandoned.

Solution 3 - C#

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

Solution 4 - C#

For XML you might use XPath...

Solution 5 - C#

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.

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

Let me know if I've misunderstood.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - C#Jamie TreworgyView Answer on Stackoverflow
Solution 2 - C#Andy SView Answer on Stackoverflow
Solution 3 - C#DanielView Answer on Stackoverflow
Solution 4 - C#Frank SchwietermanView Answer on Stackoverflow
Solution 5 - C#patjbsView Answer on Stackoverflow