简体   繁体   中英

How to get innertext into array to add custom tags around the elements in HTMLAgilityPack

I want to be able to add custom tags around just text, so say I have a node:

<p>Some text <img src="" /> more text </p>

How can I get my custom tags around 'Some text' and 'more text'? Basically I need to end up with array with the elements:

arr[0] = 'Some text'
arr[1] = 'more text'

..and then add my custom tags around them, so when the those text elements are edited, the p tag looks like

<p>Some text with edits <img src="" /> more text with edits </p>

Also, child nodes would need to be handled in the same way.

There are many ways. The simplest way it to query all text nodes:

var doc = ... get your document loaded
var textNodes = (doc.DocumentNode.SelectNodes("//text()") ?? Enumerable.Empty<HtmlNode>()).ToArray();

The // option in Xpath will find all nodes anywhere in the tree. text() will select all ot the nodes of type Text.

Since SelectNodes will return null if no nodes are found, I made sure to return an emtpy array in that case for easier further processing.

Now you can do with these textnodes what you want. Set their InnerText or InnerHtml properties to override the text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM