Get text after the span with a class with Html Agility Pack

c# html html-agility-pack

Question

I have this structure

   <span class="glyphicon glyphicon-user"></span>
                         123                       <br>

I want to get "123"

this code get between the tag if exist

var username = document.DocumentNode.SelectSingleNode("//p[@class='glyphicon-user']");
//Response.Write(username.InnerText);

but i want to get "123"

Accepted Answer

This is one possible way, based solely on the HTML sinppet posted so far :

var query = "//span[@class='glyphicon glyphicon-user']/following-sibling::text()[1]";
var username = document.DocumentNode.SelectSingleNode(query);
Console.WriteLine(username.InnerText.Trim());

Basically the XPath looks for span with certain class attribute value, and then return the nearest text node located after the span. There might be a better way depending on your exact need and the actual HTML structure.

dotnetfiddle demo

output :

123


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why