HtmlAgilityPack: problems getting contents of anchor tag inside string

Guys what I'm trying to do is the html code section below. I need the content in the anchor tag.

HtmlDocument newHtml = new HtmlDocument();
newHtml.OptionOutputAsXml = true;

var content = "<div class="business-name-container">
            <span class="tier_info"></span>
                <h3 class="title fn org">
                    <a     href="http://www.abc.com/nationwide/mip/xyz?lid=161004592" class="url link">Foo</a>
                </h3>
            </div>";

newHtml.Load(content);
HtmlNode doc = newHtml.DocumentNode;
var findContent = doc.SelectNodes("//a[@class='url link']");
foreach (var aContent in findContent)
{
   if (acontent.InnerHtml != null)
    {
           Console.WriteLine("Content: " + acontent.InnerHtml);
    }
}

But I do not get results. I want the output to be like "Content: Foo"

+3
source share
1 answer

Replace

Console.WriteLine("Content: " + acontent.InnerHtml);

WITH

Console.WriteLine("Content: " + acontent.InnerText);

Or even better is something like this

var result = acontent.DocumentNode
             .Descendants("a")
             .Where(x=>x.Attributes["class"].Value =="url link").InnerText;
+3
source

All Articles