HTML敏捷性包C#段落解析问题 [英] Html Agility Pack c# Paragraph parsing problem

查看:214
本文介绍了HTML敏捷性包C#段落解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇的我的代码的问题,我试图拉从一个页面每一个段落,但目前它只是选择的最后一段。



下面是我的代码。

 的foreach(在doc.DocumentNode.SelectNodes HtmlNode节点(// DIV [@ ID = '身体'] / p))
{
字符串文本= node.InnerText;
lblTest2.Text =文本;
}


解决方案

在你的循环,你正在服用当前节点的innerText并将其分配给的标签。你这样做的每个的节点,所以当然只能看到最后一个 - 你是不是保留了以前的



试试这个:

 的foreach(在doc.DocumentNode.SelectNodes HtmlNode节点(// DIV [@ ID ='身体'] / p ))
{
字符串文本= node.InnerText;
lblTest2.Text + =文本+ Environment.NewLine;
}


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;
}

解决方案

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;
}

这篇关于HTML敏捷性包C#段落解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆