Html Agility Pack c# Paragraph parsing problem

坚强是说给别人听的谎言 提交于 2019-12-31 04:42:04

问题


I am having a couple of issues with my code, I am trying to pull every paragraph from a page, but at the moment it is only selecting the last paragraph.

here is my code.

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@id='body']/p"))
{
  string text = node.InnerText;
  lblTest2.Text = text;
}

回答1:


In your loop you are taking the current node innerText and assigning it to the label. You do this to each node, so of course you only see the last one - you are not preserving the previous ones.

Try this:

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@id='body']/p"))
{
  string text = node.InnerText;
  lblTest2.Text += text + Environment.NewLine;
}



回答2:


IMO, XPath is no fun. I'd recommend using LINQ syntax instead:

foreach (var node in doc.DocumentNode
    .DescendantNodes()
    .Single(x => x.Id == "body")
    .DescendantNodes()
    .Where(x => x.Name == "p")) 
{
    string text = node.InnerText;
    lblTest2.Text = text;
}


来源:https://stackoverflow.com/questions/4752840/html-agility-pack-c-sharp-paragraph-parsing-problem

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