HtmlAgilityPack not looping through collection

…衆ロ難τιáo~ 提交于 2019-12-08 07:01:21

问题


I have a webpage with a collection of div's with the class "messages". I am trying to loop through them and put them into a CSV file, however I cant get the collection to loop properly. Here is the code:

string fpath = @"C:\Texts\messages.html";

HtmlDocument page = new HtmlWeb().Load(fpath);

var msgs = page.DocumentNode.SelectNodes("//div[@class='message']");

List<string> msgList = new List<string>();
foreach (var msg in msgs)
{
    msgList.Add(msg.InnerHtml);
}

The msg stays the same through each iteration of the loop. Any idea why that is?

Edit:

Sample HTML (this is 1 msg. msgs will be a collection of these)

<div class="message">
<div class="from"><span class="profile fn">Name</span></div>
<abbr class="time published" title="2012-08-11T07:02:50+0000">August 10, 2012 at 5:02 pm</abbr>
<div class="msgbody">
A message will be here.
</div>
</div>

来源:https://stackoverflow.com/questions/16973028/htmlagilitypack-not-looping-through-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!