E.D.
E.D.

Reputation: 273

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.

Upvotes: 1

Views: 311

Answers (1)

jessehouwing
jessehouwing

Reputation: 114721

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.

Upvotes: 1

Related Questions