HTML Agility Pack - Get Text From 1st STRONG Tag Inside SPAN Tag

Go To StackoverFlow.com

3

There are 5 STRONG Tags inside my SPAN Tag from my Html document. I want to know how to get the text from the first STRONG Tag inside the SPAN TAG?

Here is my code so far.

        var web = new HtmlWeb();
        var doc = web.Load(url);

        var nodes = doc.DocumentNode.SelectNodes("//span[@class='advisory_link']/strong");

        foreach (var node in nodes)
        {
            richTextBox1.Text = node.InnerHtml;
        }
2012-04-04 06:42
by guitarPH
Already got the answer! :)

        var web = new HtmlWeb();
        var doc = web.Load(url);

        var nodes = doc.DocumentNode.SelectNodes("//span[@class='advisory_link']//strong[1]");

        foreach (var node in nodes)
        {
            richTextBox1.Text = node.InnerHtml;
        }
< - guitarPH 2012-04-04 06:51
Post your answer as the answer and accept it. That way you can close your question - jessehouwing 2012-04-04 18:51
Make sure you test for null after calling SelectNodes or tag ?? new HtmlNodeCollection(null) to it. Otherwise you'll get a NullReferenceException in your foreach loop if the tag isn't found - jessehouwing 2012-04-04 18:52


1

        var nodes = doc.DocumentNode.SelectNodes("//span[@class='advisory_link']//strong[1]");

        if (nodes != null)
        {
            foreach (var node in nodes)
            {
                string Description = node.InnerHtml;
                return Description;
            }
        }

        return null;
2012-04-08 04:49
by guitarPH
Ads