本文介绍了HTMLagilitypack xpath解析数据问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从网站上获取所有表格.然后遍历所有
I''m trying to get all the tables from a website. Then go through all the
<td></td>节点. 由于某些原因,foreach循环从它经历的每个循环中获取相同的日期. HTML看起来像这样.
nodes. for some reason the foreach loop gets the same dates from every single loop it goes through. the HTML looks something like this.
<table> <tr> <td class=t>1.10</td><td class=t>2.10</td><td class=t>3.10</td><td class=t>4.10</td><td class=t>5.10</td><td class=t>6.10</td> </tr> </table>
HtmlNodeCollection weeks = doc.DocumentNode.SelectNodes("//table"); foreach(HtmlNode week in weeks) { HtmlNodeCollection tempdate = week.SelectNodes("//td[@class='t']"); for (int i = 0; i < 6; i++) { dates[i] = tempdate[i].InnerText; } }
推荐答案
您的逻辑看起来很简单. Your logic looks off. HtmlNodeCollection weeks = doc.DocumentNode.SelectNodes("//table"); foreach(HtmlNode week in weeks) { HtmlNodeCollection tempdate = week.SelectNodes("//td[@class=''t'']"); for (int i = 0; i < 6; i++) { dates[i] = tempdate[i].InnerText; } }
在您的代码中,您循环通过的每个内部循环只是在循环通过另一个HTMLNode(外部循环)时重置date []值. 您打算如何处理日期[]
In your code, every inner loop you loop through is just resetting the dates[] values when it loops through another HTMLNode (outer loop). What do you intend to do with dates[]
更多推荐
HTMLagilitypack xpath解析数据问题
发布评论