C#,Html Agility,选择div标签中的每个段落 [英] C#, Html Agility, Selecting every paragraph within a div tag

查看:126
本文介绍了C#,Html Agility,选择div标签中的每个段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div id =body_text>如何选择div标签中的每个段落? 
< p>嗨< / p>
< p>请帮助我< / P>
< p> Thankyou< / P>

我在程序中下载并引用了Html Agility,我需要的是段落。可能有不定数量的段落,并且有不同的div标签加载,但我只需要body_text内的内容。然后我认为这可以存储为一个字符串,然后我想写入一个.txt文件供以后参考。谢谢。

解决方案

您的案件的有效XPATH是 // div [@ id ='body_text' ] / p

  foreach(HtmlNode node in yourHTMLAgilityPackDocument.DocumentNode.SelectNodes(// div [ @ b = b)
{
string text = node.InnerText; //这就是你正在寻找的文本
}


How can I select every paragraph in a div tag for example.

<div id="body_text">
<p>Hi</p>
<p>Help Me Please</P>
<p>Thankyou</P>

I have got Html Agility downloaded and referenced in my program, All I need is the paragraphs. There may be a variable number of paragraphs and there are loads of different div tags but I only need the content within the body_text. Then I assume this can be stored as a string which I then want to write to a .txt file for later reference. Thankyou.

解决方案

The valid XPATH for your case is //div[@id='body_text']/p

foreach(HtmlNode node in yourHTMLAgilityPackDocument.DocumentNode.SelectNodes("//div[@id='body_text']/p")
{
  string text = node.InnerText; //that's the text you are looking for
}

这篇关于C#,Html Agility,选择div标签中的每个段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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