SlideShare a Scribd company logo
1 of 13
CsQuery
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.
Howard 2013/11/07
Why CsQuery?
•

Standards Compliant HTML parsing
 CSS selectors and jQuery make it really easy to access and manipulate HTML on the
client.

•

CSS3 Selectors and jQuery methods
 CsQuery implements all CSS2 and CSS3 selectors and filters, and a comprehensive
DOM model.

•

Fast, indexed CSS selectors
 The CSS selector engine fully indexes each document on tag name, id, class, and
attribute.

•

It's incredibly easy
CQ vs jQuery
•

dom["<div></div>"] <===> $('<div></div>')

•

dom["div"] <===> $('div')

•

dom.Select("div") <===> $('div')

•

dom["div"][0] <===> dom.Select("div")[0] <===> $('div')[0]
CQ
•

CQ object exposes an IEnumerable<IDomObject> interface
 You can use LINQ to simplify many operations.
 You have all the tools that you're used to from jQuery.
 public partial class CQ : IEnumerable<IDomObject>

•

•

Like in jQuery, each CQ object is made up of DOM elements.

In CsQuery, the basic node is an IDomObject
 is analagous to an HTML element or other node (like a text node or comment node)
CQ
CQ dom = "<div>Hello world! <b>I am feeling bold!</b> What about <b>you?</b></div>";
•

use CSS to choose first node only
 string bold = dom["div > b:first-child"].Text();

•

use LINQ First to get the first item, and the DOM node "InnerText" method
 string bold = dom["b"][0].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
 string bold = dom["b"].Contents()[0].NodeValue
Create
•

Create from a string of HTML, a TextReader, a Stream, or an existing CQ object or DOM elements
 var dom = CQ.Create(html);

•

Create from a URL (synchronously)
 var dom = CQ.CreateFromUrl("http://www.jquery.com");

•

Create from a URL (asynchronously)
 CQ. CreateFromUrlAsync("http://www.jquery.com", successDelegate, failureDelegate);
 IPromise promise = CQ. CreateFromUrlAsync("http://www.jquery.com");
 returns an IPromise object, which can be used to manage resolution of deferred events without blocking the
code flow
Output as HTML
•

Render the entire DOM
 string html = dom.Render();

•

You can render any DOM element individually
 string elementHtml = dom[2].Render();

•

You can render just the elements that are part of the selection
 string selectionHtml = dom[".just-this-class"].RenderSelection();
Manipulate the DOM with jQuery methods
dom.Select("div > span")
.Eq(1)
.Text("Change the text content of the 2nd span child of each div");

•

Use Find (like in jQuery) to access only child elements of a selection:
 var childSpans = dom["body"].Find(":first-child");

•

CssSet VS CSS

 rowsWithClass
.CssSet(new {
width="100px",
height=20
});

 rowsWithClass.CssSet("{"width":"100px", "height":"100px"}")
 rowsWithClass.Css("width",100).Css("height","20px");
Accessing DOM elements directly
•

The property indexer is overloaded as a simple list element indexer returning
the DOM element at that position, just like $(...)[n].

•

IDomObject element = dom[0];

•

string id = element.Id;

•

string classes = element.ClassName;
Promises
•

More recent versions jQuery introduced a "deferred" object for managing callbacks
using a concept called Promises.

•

The CQ.CreateFromUrlAsync method can return an IPromise<ICsqWebResponse>
object.
Promises sample
•
Reference
•

https://github.com/jamietre/CsQuery
Q&A

More Related Content

What's hot

Introduction to j query
Introduction to j queryIntroduction to j query
Introduction to j query
thewarlog
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
Kevin Faustino
 

What's hot (20)

JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
JQuery
JQueryJQuery
JQuery
 
JavaScript and jQuery Basics
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery Basics
 
How dojo works
How dojo worksHow dojo works
How dojo works
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
 
Introduction to j query
Introduction to j queryIntroduction to j query
Introduction to j query
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRails
 
jQuery
jQueryjQuery
jQuery
 
Introductory css and j query
Introductory css and j queryIntroductory css and j query
Introductory css and j query
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Introduction to html & css
Introduction to html & cssIntroduction to html & css
Introduction to html & css
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
Web components
Web componentsWeb components
Web components
 
Ng init | EPI Sousse
Ng init | EPI SousseNg init | EPI Sousse
Ng init | EPI Sousse
 
jQuery
jQueryjQuery
jQuery
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
 
Introduction to j_query
Introduction to j_queryIntroduction to j_query
Introduction to j_query
 
jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 

Viewers also liked

Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
Matjaž Lipuš
 

Viewers also liked (20)

CSS3: Ripe and Ready to Respond
CSS3: Ripe and Ready to RespondCSS3: Ripe and Ready to Respond
CSS3: Ripe and Ready to Respond
 
HTML basics
HTML basicsHTML basics
HTML basics
 
Html javascript
Html javascriptHtml javascript
Html javascript
 
CSS3 Implementable Features
CSS3 Implementable FeaturesCSS3 Implementable Features
CSS3 Implementable Features
 
[heweb11] CSS3 Makeover
[heweb11] CSS3 Makeover[heweb11] CSS3 Makeover
[heweb11] CSS3 Makeover
 
Dominik Veselý - Vše co jste kdy chtěli vědět o CI a báli jste se zeptat
Dominik Veselý - Vše co jste kdy chtěli vědět o CI a báli jste se zeptatDominik Veselý - Vše co jste kdy chtěli vědět o CI a báli jste se zeptat
Dominik Veselý - Vše co jste kdy chtěli vědět o CI a báli jste se zeptat
 
HTML Basics 2 workshop
HTML Basics 2 workshopHTML Basics 2 workshop
HTML Basics 2 workshop
 
Basics of html for web development by software outsourcing company india
Basics of html for web development   by software outsourcing company indiaBasics of html for web development   by software outsourcing company india
Basics of html for web development by software outsourcing company india
 
Introduction to php 2
Introduction to php   2Introduction to php   2
Introduction to php 2
 
JS App Architecture
JS App ArchitectureJS App Architecture
JS App Architecture
 
HTML and CSS Basics
HTML and CSS BasicsHTML and CSS Basics
HTML and CSS Basics
 
Mastering CSS3 Selectors
Mastering CSS3 SelectorsMastering CSS3 Selectors
Mastering CSS3 Selectors
 
Oocss & progressive css3 selectors
Oocss & progressive css3 selectorsOocss & progressive css3 selectors
Oocss & progressive css3 selectors
 
CSS3
CSS3CSS3
CSS3
 
Basics Of Html
Basics Of HtmlBasics Of Html
Basics Of Html
 
Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
 
[@NaukriEngineering] CSS4 Selectors – Part 1
[@NaukriEngineering] CSS4 Selectors – Part 1[@NaukriEngineering] CSS4 Selectors – Part 1
[@NaukriEngineering] CSS4 Selectors – Part 1
 
CSS3: Ripe and Ready
CSS3: Ripe and ReadyCSS3: Ripe and Ready
CSS3: Ripe and Ready
 
Css3
Css3Css3
Css3
 
Html basics
Html basicsHtml basics
Html basics
 

Similar to 20131108 cs query by howard

Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
Laila Buncab
 
J query presentation
J query presentationJ query presentation
J query presentation
akanksha17
 
J query presentation
J query presentationJ query presentation
J query presentation
sawarkar17
 
Jquery dojo slides
Jquery dojo slidesJquery dojo slides
Jquery dojo slides
helenmga
 

Similar to 20131108 cs query by howard (20)

J query module1
J query module1J query module1
J query module1
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
JavaScript front end performance optimizations
JavaScript front end performance optimizationsJavaScript front end performance optimizations
JavaScript front end performance optimizations
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSS
 
Web components
Web componentsWeb components
Web components
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
The Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has ArrivedThe Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has Arrived
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
PhDigital 2020: Web Development
PhDigital 2020: Web DevelopmentPhDigital 2020: Web Development
PhDigital 2020: Web Development
 
A brave new web - A talk about Web Components
A brave new web - A talk about Web ComponentsA brave new web - A talk about Web Components
A brave new web - A talk about Web Components
 
J query presentation
J query presentationJ query presentation
J query presentation
 
J query presentation
J query presentationJ query presentation
J query presentation
 
Polymer and web component
Polymer and web componentPolymer and web component
Polymer and web component
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Jquery dojo slides
Jquery dojo slidesJquery dojo slides
Jquery dojo slides
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Introduction to Jquery
Introduction to JqueryIntroduction to Jquery
Introduction to Jquery
 

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Recently uploaded

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

20131108 cs query by howard

  • 1. CsQuery 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. Howard 2013/11/07
  • 2. Why CsQuery? • Standards Compliant HTML parsing  CSS selectors and jQuery make it really easy to access and manipulate HTML on the client. • CSS3 Selectors and jQuery methods  CsQuery implements all CSS2 and CSS3 selectors and filters, and a comprehensive DOM model. • Fast, indexed CSS selectors  The CSS selector engine fully indexes each document on tag name, id, class, and attribute. • It's incredibly easy
  • 3. CQ vs jQuery • dom["<div></div>"] <===> $('<div></div>') • dom["div"] <===> $('div') • dom.Select("div") <===> $('div') • dom["div"][0] <===> dom.Select("div")[0] <===> $('div')[0]
  • 4. CQ • CQ object exposes an IEnumerable<IDomObject> interface  You can use LINQ to simplify many operations.  You have all the tools that you're used to from jQuery.  public partial class CQ : IEnumerable<IDomObject> • • Like in jQuery, each CQ object is made up of DOM elements. In CsQuery, the basic node is an IDomObject  is analagous to an HTML element or other node (like a text node or comment node)
  • 5. CQ CQ dom = "<div>Hello world! <b>I am feeling bold!</b> What about <b>you?</b></div>"; • use CSS to choose first node only  string bold = dom["div > b:first-child"].Text(); • use LINQ First to get the first item, and the DOM node "InnerText" method  string bold = dom["b"][0].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  string bold = dom["b"].Contents()[0].NodeValue
  • 6. Create • Create from a string of HTML, a TextReader, a Stream, or an existing CQ object or DOM elements  var dom = CQ.Create(html); • Create from a URL (synchronously)  var dom = CQ.CreateFromUrl("http://www.jquery.com"); • Create from a URL (asynchronously)  CQ. CreateFromUrlAsync("http://www.jquery.com", successDelegate, failureDelegate);  IPromise promise = CQ. CreateFromUrlAsync("http://www.jquery.com");  returns an IPromise object, which can be used to manage resolution of deferred events without blocking the code flow
  • 7. Output as HTML • Render the entire DOM  string html = dom.Render(); • You can render any DOM element individually  string elementHtml = dom[2].Render(); • You can render just the elements that are part of the selection  string selectionHtml = dom[".just-this-class"].RenderSelection();
  • 8. Manipulate the DOM with jQuery methods dom.Select("div > span") .Eq(1) .Text("Change the text content of the 2nd span child of each div"); • Use Find (like in jQuery) to access only child elements of a selection:  var childSpans = dom["body"].Find(":first-child"); • CssSet VS CSS  rowsWithClass .CssSet(new { width="100px", height=20 });  rowsWithClass.CssSet("{"width":"100px", "height":"100px"}")  rowsWithClass.Css("width",100).Css("height","20px");
  • 9. Accessing DOM elements directly • The property indexer is overloaded as a simple list element indexer returning the DOM element at that position, just like $(...)[n]. • IDomObject element = dom[0]; • string id = element.Id; • string classes = element.ClassName;
  • 10. Promises • More recent versions jQuery introduced a "deferred" object for managing callbacks using a concept called Promises. • The CQ.CreateFromUrlAsync method can return an IPromise<ICsqWebResponse> object.
  • 13. Q&A