Get value of an attribute by HtmlAgilityPack

c# html-agility-pack


<div class="vcard-names-container py-3 js-sticky js-user-profile-sticky-fields " style="position: static;">
  <h1 class="vcard-names">
    <span class="vcard-fullname d-block" itemprop="name">Name 001</span>
    <span class="vcard-username d-block" itemprop="additionalName">Name 002</span>

Hello, I would like to know how to retrieve the names of this structure ('Name 001' and 'Name 002') I did several tests but I could not parse these values, how do I achieve this?

(Note: Sorry for my bad english)

Accepted Answer

You can use XPath to query the document nodes to find the nodes you are looking for:

static void Main(string[] args)
    var html = @"<div class=""vcard - names - container py - 3 js - sticky js - user - profile - sticky - fields "" style=""position: static; "">
           < h1 class=""vcard-names"">
            <span class=""vcard-fullname d-block"" itemprop=""name"">Name 001</span>
            <span class=""vcard-username d-block"" itemprop=""additionalName"">Name 002</span>

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

    var names = doc.DocumentNode.SelectNodes("//span").Select(x => x.InnerText);
    foreach (var name in names)

Popular Answer

This might do the trick for you

HtmlDocument doc = new HtmlDocument();
foreach(HtmlNode Spans in doc.DocumentNode.SelectNodes("//span"))


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