Jacqueline
Jacqueline

Reputation: 491

Htmlagilitypack - Get all text + links inside of text

I have a method which extracts text blocks that are over XX amount of words. The problem is that it wont return links inside of that text.

My method:

public string getAllTextHTML(string _html)
{
  string _allText = "";
  try
  {
    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    document.LoadHtml(_html);

    document.DocumentNode.Descendants()
      .Where(n => n.Name == "script" || n.Name == "style")
      .ToList()
      .ForEach(n => n.Remove());

    RemoveComments(document.DocumentNode);

    var root = document.DocumentNode;
    var sb = new StringBuilder();
    foreach (var node in root.DescendantNodesAndSelf())
    {
      if (!node.HasChildNodes)
      {
        string text = node.InnerHtml;

        if (!string.IsNullOrEmpty(text))
        {
          int antalOrd = WordCounting.CountWords1(text);

          if (antalOrd > 25)
          {
            text = System.Web.HttpUtility.HtmlDecode(text);
            sb.AppendLine(text.Trim());
          }  
        } 
      }
    }

    _allText = sb.ToString();
  }
  catch (Exception)
  {
  }

  _allText = System.Web.HttpUtility.HtmlDecode(_allText);
  return _allText;
}

How could i make this also get me the links within the text?

Upvotes: 2

Views: 1956

Answers (1)

Danilo Vulović
Danilo Vulović

Reputation: 3063

I guess that following line makes a problem:

if (!node.HasChildNodes)

because, link (anchor) is htlm tag and you exclude html tags which have anchor tag as a child.

Here is simple example that returns link:

String html = "<p>asdf<a href='#'>Test</a>asdfasd</p>";

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

String p = (from x in doc.DocumentNode.Descendants()
            where x.Name == "p"
            select x.InnerHtml).FirstOrDefault();

Upvotes: 1

Related Questions