【发布时间】:2017-09-11 20:55:20
【问题描述】:
我有一个用于解析 html 的 foreach 循环,并且我正在使用 xpath。我需要的是关注
<p class="section">sectiontext1</p>
<p class="subsection">subtext1</p> ----need this in first loop
<p class="subsection">subtext2</p> ---need this in first loop
<p class="section">sectiontext2</p>
<p class="subsection">subtext11</p> ---need this in second loop
<p class="subsection">subtext22</p> ---- need this in second loop
<p class="section">sectiontext3</p>
foreach (HtmlNode sectionNode in htmldocObject.DocumentNode.SelectNodes("//p[@class='section']"))
{
count=count+2;
string text1 = sectionNode.InnerText;
foreach (HtmlNode subSectionNode in htmldocObject.DocumentNode.SelectNodes("//p[@class='subsection'][following-sibling::p[@class='section'][1] and preceding-sibling::p[@class='section'][2]]"))
{
string text = subSectionNode.InnerText;
}
}
我想要做的是循环浏览各个部分并找到特定部分下的每个小节,进行一些处理,然后移动到下一部分以查找该特定部分下的小节。
【问题讨论】:
-
I have a foreach loop to parse html and i am using xpath for it.请出示该代码。 -
你为什么要用 xpath 来做这个?您使用的是
XSLT、XPathNavigator还是LINQ to XML? -
@mjwills 更新帖子以添加 foreach 循环代码
-
@Matthew Whited - 我在我的 .net c# 应用程序中使用 HTMLAgility 包。我想要做的是网络抓取法规内容并将其转储到 excel 表中。