2011-09-11 33 views
13

我需要能夠在我的C#應用​​程序中簡單地指定HTML元素。我只會使用Linq到Sql,但這需要可配置/可串行化爲一個字符串。我當然可以使用XPath,但在這一點上像Sizzle這樣的東西對於大多數人來說更加自然。在C#中有沒有Sizzle/jQuery選擇器的實現?

任何人都知道.net中是否存在嘶嘶聲選擇器實現?

+6

[CsQuery](http://github.com/jamietre/CsQuery)似乎非常有前途。我還沒有使用它,只是閱讀了這個問題後 - 瀏覽了這個問題。不應該關閉。 – robert4

回答

18

葉普,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"); 
相關問題