2013-02-07 68 views
3

我的應用程序應該解析html並將內容加載到列表框中。我能夠通過webclient獲取html,但被卡在解析它。
我聽說過Htmlagilitypack和Fizzler,但找不到任何教程或例子的使用。使用C#解析html 8 for windows樣式的應用程序,XAML

我需要一些幫助,將「first_content」和「second_content」從下面顯示的html文檔中抓取到列表框中。

<html> 
<body> 
<div> 
<section> 
<article> 
    <header> 
     <hgroup> 
      <h1> 
       first_content 
      </h1> 
     </hgroup> 
    </header> 
    <ul> 
     <li> 
      second_content 
     </li> 
    </ul> 
</article> 
</section> 
</div> 
</body> 
</html> 
+1

對於「你做了什麼?系列,你嘗試用一個原始的XmlReader嗎? –

+1

我有點佩服[努力](http://stackoverflow.com/posts/14756076/edit/05cfc11a-9a36-4fde-90da-8422f82b9f94)你格式化你的問題,但請閱讀[格式化常見問題] (http://stackoverflow.com/editing-help),以適當的方式做起來容易得多。 – GSerg

回答

3

HtmlAgilityPack是去了,我一直在使用它在WCF的Windows Phone和WinRT的現在,總的成功,對於一個教程檢查this blog post

+0

感謝您的博客。但是博客中的文章並不適用於winrt。例如,「var document = webGet.Load(url);」 webGet沒有「Load」方法來加載URL。相反,我使用了webGet.LoadFromWebAsync(「http://www.something.com」),它正在工作。 For,document.DocumentNode.SelectNodes(「// meta」);我正在爲「DocumentNode」添加紅色下劃線。我無法繼續。 你能幫我一個教程,只是指向metro風格的應用程序? –

0

您可以使用XPath的方式。例如...

var html = "<html><body><div><section><article><header><hgroup><h1>first_content</h1></hgroup></header><ul><li>second_content</li></ul></article> </section></div></body></html>"; 
var doc = new XmlDocument(); 
doc.LoadXml(html); 
var txt1 = doc.SelectSingleNode("/html/body/div/section/article/header/hgroup/h1").InnerText; 
var txt2 = doc.SelectSingleNode("/html/body/div/section/article/ul/li").InnerText; 
相關問題