2013-02-19 47 views
0

我寫了一個應用程序,從表中取值並操作它們,我的問題是在我想要的表(無ID,類)之前有2個表。我想跳過他們去第三張桌子。我的代碼:如何跳過表格使用HtmlAgilityPack

 HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table"); 
     HtmlNodeCollection rows = tables[2].SelectNodes(".//tr"); 

     foreach (HtmlNode item in rows) 
     {  
     /// my code// 

     } 

我以爲代碼:表[2]指進入第三個表,但INFACT意味着需要3個表,有沒有辦法來定義spacific表或到表? (表中沒有id或類名)

回答

1

我想下面的代碼,將有助於你對這個...

HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table[3]"); 
HtmlNodeCollection rows = tables.SelectNodes(".//tr"); 

"//table[3]":它定義了3臺

+0

如果我這樣做:HtmlNodeCollection tables = doc.DocumentNode.SelectNodes(「// table [3]」); HtmlNodeCollection rows = tables [2] .SelectNodes(「.// tr」);我得到了錯誤:索引超出範圍。必須是非負數且小於集合的大小。參數名稱:索引 – ogsMC 2013-02-19 13:42:33

+0

@ogsMC:更改您的代碼從HtmlNodeCollection rows = tables [2] .SelectNodes(「.// tr」);至HtmlNodeCollection rows = tables.SelectNodes(「。// tr」);將得到工作.... – Pandian 2013-02-19 13:43:52

+0

錯誤...我必須給他的索引後的「表」。 :*( – ogsMC 2013-02-19 13:47:53

0

您只需指定表的索引:HtmlNodeCollection tables = doc.DocumentNode.SelectNodes(「// table [2]」);

+0

如果我這樣做:HtmlNodeCollection表= doc.DocumentNode.SelectNodes(「/ /表[2]「); HtmlNodeCollection rows = tables [2] .SelectNodes(「.// tr」); 我得到了錯誤:索引超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引 – ogsMC 2013-02-19 13:38:33

+0

如果您打算獲得第三張表,您只能在xpath查詢中執行此操作。 您不必在此行中指定它: tables.SelectNodes(「.// tr」); – 2013-02-19 16:43:47