Only all text nodes are iterated by HTMLAgilityPack.

html-agility-pack

Question

I only want to iterate through the text nodes in the following HTML sample. Please tell me. Thanks.

<div>
   <div>
      Select your Age:
      <select>
          <option>0 to 10</option>
          <option>20 and above</option>
      </select>
   </div>
   <div>
       Help/Hints:
       <ul>
          <li>This is required field.
          <li>Make sure select the right age.
       </ul>
      <a href="#">Learn More</a>
   </div>
</div>

Result:

  1. Choose your Age:
  2. 0 to 10
  3. 20 or more
  4. Help/Hints:
  5. This field is necessary.
  6. Make sure you choose the appropriate age.
  7. Study More
1
5
6/14/2011 2:03:27 PM

Accepted Answer

Possibly like this:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(yourHtmlFile);

    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']"))
    {
        Console.WriteLine(node.InnerText.Trim());
    }

This will be output:

Select your Age:
0 to 10
20 and above
Help/Hints:
This is required field.
Make sure select the right age.
Learn More
20
6/14/2011 3:07:45 PM

Popular Answer

There was a lot of CSS and Javascript when I tested @Simon Mourier's response on the Google homepage, so I created an additional filter to get rid of it:

    public string getBodyText(string html)
    {
        string str = "";

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

        try
        {
            // Remove script & style nodes
            doc.DocumentNode.Descendants().Where( n => n.Name == "script" || n.Name == "style" ).ToList().ForEach(n => n.Remove());

            // Simon Mourier's Answer
            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']"))
            {
                str += node.InnerText.Trim() + " ";
            }
        }
        catch (Exception)
        {
        }

        return str;
    }


Related Questions





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