Getting the text from a node using HtmlAgilityPack

喜夏-厌秋 提交于 2019-12-04 18:31:37

What do you want to extract, nodes or a string?

If you want nodes, "I want <em>this</em> text." is an XML fragment consisting at the top level of two text nodes and an <em> element, which has a text node child. Since it has multiple nodes at the top level, you need to use SelectNodes("xpath expression a la @Alejandro") rather than SelectSingleNode() to extract them.

If you want a string, again you need to use SelectNodes(); and then iterate over the selected nodes and concatenate the outerHTML of each one. See here for a good example of something similar.

Also, it's a little unclear from your example what XPath expression would in general give you what you want. E.g. do you want everything after the initial <p>...</p> under <div class="top">? Or do you want all text under the <div> except all <p> elements? Or maybe something else? Of course if @Alejandro's XPath expressions work for you then it's already well-specified enough.

/div[@class='top']/p[.='Blah.']/following-sibling::node()

or

/div[@class='top']/node()[not(self::p)]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!