问题
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