开发者

Finding <div> in html using C#

开发者 https://www.devze.com 2022-12-20 01:55 出处:网络
I\'ve seen some r开发者_StackOverflow社区esults on StackOverflow already on a similar topic and they all recommended the HTML Agility Pack. I\'ve also found a few examples on it too, but it isn\'t wor

I've seen some r开发者_StackOverflow社区esults on StackOverflow already on a similar topic and they all recommended the HTML Agility Pack. I've also found a few examples on it too, but it isn't working. My current code:

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml("http://stackoverflow.com/");
        List<string> facts = new List<string>();
        foreach (HtmlNode li in doc.DocumentNode.SelectNodes("//div"))
        {
            facts.Add(li.InnerText);
            foreach (String s in facts)
            {
                textBox1.Text += s + "/n";
            }
        }

I get the error Null reference was unhandled on doc.DocumentNode.SelectNodes("//div").

Also, as another question, how do I find a specific div with a name?


as to q1: it looks right to me. Are you certain the document is loaded properly..

r.e. q2: use xpath: div[@id='idToFind']

0

精彩评论

暂无评论...
验证码 换一张
取 消