HTML Agility Pack - Grab Text after a node

c# html html-agility-pack


I have some HTML that I'm parsing using C#

The sample text is below, though this is repeated about 150 times with different records

<strong>Title</strong>: Mr<br>
<strong>First name</strong>: Fake<br>
<strong>Surname</strong>: Guy<br>

I'm trying to get the text in an array which will be like

customerArray [0,0] = Title
customerArray [0,1] = Mr
customerArray [1,0] = First Name
customerArray [1,1] = Fake
customerArray [2,0] = Surname
customerArray [2,1] = Guy

I can get the text in the array but I'm just having trouble getting the text after the STRONG closing tab up until the BR tag then finding the next STRONG tag

any help would be appreciated

3/10/2017 5:32:31 AM

Accepted Answer

You can use XPath following-sibling::text()[1] to get text node located directly after each strong. Here is a minimal but complete example :

var raw = @"<div>
<strong>Title</strong>: Mr<br>
<strong>First name</strong>: Fake<br>
<strong>Surname</strong>: Guy<br>
var doc = new HtmlDocument();
foreach(HtmlNode node in doc.DocumentNode.SelectNodes("//strong"))
    var val = node.SelectSingleNode("following-sibling::text()[1]");
    Console.WriteLine(node.InnerText + ", " + val.InnerText);

dotnetfiddle demo

output :

Title, : Mr
First name, : Fake
Surname, : Guy

You should be able to remove the ":" by doing simple string manipulation, if needed...

3/10/2017 6:11:45 AM

Popular Answer

<strong> is a common tag, so something specific for the sample format you provided.

var html = @"
<strong>First name</strong><em>italic</em>: Fake<br>
<strong>Bold</strong> <a href='#'>hyperlink</a><br>.
<strong>bold</strong> <br>

<strong>Title</strong>: Mr<BR>
<strong>First name</strong>: Fake<br>
<strong>Surname</strong>: Guy<br>

var document = new HtmlDocument();
// 1. <strong>
var strong = document.DocumentNode.SelectNodes("//strong");
if (strong != null)
    foreach (var node in strong.Where(
        // 2. followed by non-empty text node
        x => x.NextSibling is HtmlTextNode
        && !string.IsNullOrEmpty(x.NextSibling.InnerText.Trim())
        // 3. followed by <br>
        && x.NextSibling.NextSibling is HtmlNode
        && x.NextSibling.NextSibling.Name.ToLower() == "br"))
        Console.WriteLine("{0} {1}", node.InnerText, node.NextSibling.InnerText);

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow