Get value of an attribute by HtmlAgilityPack

c# html-agility-pack

Question

<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>
  </h1>
</div>

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>
          </h1>
        </div>";

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

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

Popular Answer

This might do the trick for you

HtmlDocument doc = new HtmlDocument();
doc.Load(YourHTMLFile);
foreach(HtmlNode Spans in doc.DocumentNode.SelectNodes("//span"))
{
    Console.WriteLine(Spans.InnerText);
}


Related

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