Skip to content Skip to sidebar Skip to footer

Iterate With All Elements And Get Text?

I am using the follow code to get all text from a page into a List HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(content); fore

Solution 1:

This can be done rather easily using only functions included in the HAP.

HtmlDocument doc = new HtmlWeb().Load("http://www.google.com");
List<string> words = doc.DocumentNode.DescendantNodes()
        .Where(n => n.NodeType == HtmlNodeType.Text
          && !string.IsNullOrWhiteSpace(HtmlEntity.DeEntitize(n.InnerText))
          && n.ParentNode.Name != "style" && n.ParentNode.Name != "script")
        .Select(n => HtmlEntity.DeEntitize(n.InnerText).Trim())
        .Where(s => s.Length > 2).ToList();

The result is a list of words with a length of more than 2 and everything already escaped, so no need for WebUtility.

Post a Comment for "Iterate With All Elements And Get Text?"