我们在Word中创建了一个绝对巨大的帮助文档,该文档用于生成更加大规模和无差别的HTM文档。使用C#和这个库,我只想在我的应用程序的任何位置抓取并显示这个文件的一部分。部分被划分成这样:使用Html Agility Pack获取两个HTML标签之间的内容
<!--logical section starts here -->
<div>
<h1><span style='mso-spacerun:yes'></span><a name="_Toc325456104">Section A</a></h1>
</div>
<div> Lots of unnecessary markup for simple formatting... </div>
.....
<!--logical section ends here -->
<div>
<h1><span style='mso-spacerun:yes'></span><a name="_Toc325456104">Section B</a></h1>
</div>
按理来说,有一个H1
在一个a
标签的栏目名称。我想从包含div的外部选择所有内容,直到遇到另一个h1
并排除该div。
- 各部分名称位于一个
<a>
标签下h1
其中有多个孩子(每次约6) - 逻辑部分标有注释
- 这些意见不实际的文档 中存在
我尝试:
var startNode = helpDocument.DocumentNode.SelectSingleNode("//h1/a[contains(., '"+sectionName+"')]");
//go up one level from the a node to the h1 element
startNode=startNode.ParentNode;
//get the start index as the index of the div containing the h1 element
int startNodeIndex = startNode.ParentNode.ChildNodes.IndexOf(startNode);
//here I am not sure how to get the endNode location.
var endNode =?;
int endNodeIndex = endNode.ParentNode.ChildNodes.IndexOf(endNode);
//select everything from the start index to the end index
var nodes = startNode.ParentNode.ChildNodes.Where((n, index) => index >= startNodeIndex && index <= endNodeIndex).Select(n => n);
正弦我一直没能找到这方面的文档,我不知道如何从我的开始节点到下一个h1元素。任何建议,将不胜感激。
不错。我不得不稍微修改过滤器,因为我在文档中有多个部分名称。我结束了使用'HtmlNode startNode = helpDocument.DocumentNode.Descendants(“h1”)。其中(d => d.InnerText.Contains(SectionName))。FirstOrDefault();并从那里移动到父节点。其余的工作完美。谢谢 – Rondel
优秀。我很高兴工作。 –