HtmlAgilityPack : Issues getting content of anchor tag within a string

c# html-agility-pack html-parsing

Question

Guys what i'm trying to do is I've a section of a html code listed below. I need the content within 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'm not getting the results. I want to the output to be as "Content : Foo"

Popular Answer

Replace

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

With

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

Or even better something like this

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



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