CsQuery is a jQuery port for .NET 4. It implements all CSS2 & CSS3 selectors, all the DOM manipulation methods of jQuery, and some of the utility methods. The majority of the jQuery test suite (as of 1.6.2) has been ported to C#.
CSS selectors and jQuery make it really easy to access and manipulate HTML on the client. There's no reason it should be any more difficult to do the same thing with some arbitrary HTML on the server. It's a simple as that. Use it in web projects to do post-processing on HTML pages before they're served, for web scraping, parsing templates, and more.
CsQuery uses a C# port of the validator.nu HTML parser. This is the same code used in the Gecko browser engine. CsQuery will create an identical DOM from the same source as any Gecko-based browser. You should expect excellent results for handling both valid and invalid markup.
CsQuery implements all CSS2 and CSS3 selectors and filters, and a comprehensive DOM model. You can use all the same jQuery (and DOM element) methods you're familiar with to traverse and manipulate the DOM.
The CSS selector engine fully indexes each document on tag name, id, class, and attribute. The index is subselect-capable, meaning that complex selectors will still be able to take advantage of the index (for any part of the selector that's indexed). Performance of selectors compared to other existing C# HTML parsing libraries is orders of magnitude faster.
What's more, the entire test suite from Sizzle (the jQuery CSS selector engine) and jQuery (1.6.2) has been ported from Javascript to C# to cover this project.
Pretty much everything you need is in the CQ object, which is designed to work like a jQuery object. Assigning a string to a CQ object parses it. The property indexer ['...'] runs a CSS selector,
and returns new CQ object, like $('...') using jQuery. Finally, the Render method writes the DOM back to a string. From a CQ object, you
have access to the complete jQuery API to traverse and manipulate your document, as well as an extensive browser DOM model.
Here's a basic example of parsing HTML, selecting something, altering it, and rendering it back to a string.
CQ dom = "<div>Hello world! <b>I am feeling bold!</b> What about <b>you?</b></div>";
CQ bold = dom["b"]; /// find all "b" nodes (there are two in this example)
> bold.ToList()
> Count = 2
> [0]: {<b>...</b>}
> [1]: {<b>...</b>}
> bold.First().RenderSelection()
> "<b>I am feeling bold!</b>"
string boldText = bold.Text(); /// jQuery text method;
> boldText
> "I am feeling bold! you?"
bold.Remove(); /// jQuery Remove method
string html = dom.Render();
> html
> "<div>Hello world! What about </div>"
There are other ways to create CQ objects, run selectors, and change the DOM. You can also use the property indexer
like an array indexer, e.g. dom[0] returns the first element in your selection. If there is one, that is!
Using the LINQ method dom.FirstOrDefault() might be a better choice for many situations. In javascript, you'd often test the
Length property of a selection to see if there were any results. The CQ object exposes an IEnumerable<IDomObject> interface,
so you can use LINQ to simplify many operations. But you still have all the tools that you're used to from jQuery, too.
Like in jQuery, each CQ object is made up of DOM elements. In CsQuery, the basic node is an IDomObject and is analagous to
an HTML element or other node (like a text node or comment node). Most of the typical HTML element methods are available.
So, using these alternatives, to obtain only the first bolded item from the example above:
use CSS to choose first node only
string bold = dom["div > b:first-child"].Text();
use jQuery CSS filter extensions to return the first item in the selection
string bold = dom["b:first"].Text();
use LINQ First to get the first item, and the DOM node "InnerText" method
string bold = dom["b"].First().InnerText;
use indexer to get the first item, and "Select" instead of the indexer to make it more readable
string bold = dom.Select("b")[0].InnerText;
Use jQuery "contents" method to return the text node children, the indexer to get the first, and the DOM node "nodeValue" method to get the contents of a text node
string bold = dom["b"].Contents()[0].NodeValue
Each of these returns the same thing: "I am feeling bold!"
Latest release: Version 1.3.4 (February 5, 2013)
To install the latest release from NuGet package manager:
PM> Install-Package CsQuery
To install manually, add a reference to CsQuery.DLL. There are no external dependencies.
This repository contains a submodule for HtmlParserSharp. This configuration has been chosen to allow the HTML parser project to be completely independent of CsQuery, while still allowing CsQuery to include it directly and compile to a single DLL.
To clone the repostory with the submodule, you need to take an extra step. First create a clone as usual:
git clone https://github.com/jamietre/CsQuery.git csquery
Next change to the repo folder, and initialize and clone the submodule.
cd csquery
git submodule update --init -f
You should be able to compile everything now. If you have any trouble initializing the submodule, just delete the submodule folder ../source/CsQuery/HtmlParserSharp and run the submodule init command again.
The current release is 1.3.4. This is a bug fix release:
- Handle out-of-bounds character set changes
- Allow changing character set via meta tag outside of HEAD
- Allow non-alpha ID selectors
See the change log for details.
The last major release is 1.3.0. This release implements a new HTML5-compliant parser.
- Release notes for 1.3: Read details on the new features and other changes. You can also see all CsQuery release notes at once.
- Detailed change log: see more more detail about specific changes, or notes on changes made to the source since the last release.
