Will I use HtmlDocument even I want to parse the HTML string using HtmlAglityPack ?

Posted by skhan on Stack Overflow See other posts from Stack Overflow or by skhan
Published on 2009-07-07T15:29:25Z Indexed on 2010/03/31 10:03 UTC
Read the original article Hit count: 194

Filed under:
|
|

Hi everyone,

I'm working in C#. I'm trying to extract the first instance of img tag from a HTML string (which is actually a post data).

This is my code:

 private string GrabImage(string htmlContent)
 {
    String firstImage;

    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.LoadHtml(htmlContent);
    HtmlAgilityPack.HtmlNode imageNode = htmlDoc.DocumentNode.SelectSingleNode("//img");
    if (imageNode != null)
    {
        return firstImage = imageNode.ToString();          
    }
    else
        return firstImage=" ";
}

But it gets null in htmlDoc, will I use the HtmlDocument type even if I'm trying to parse the HTML from a string ?

P.S btw is it the correct way of grabbing the first instance of image tag from my HTML string?

© Stack Overflow or respective owner

Related posts about parsing

Related posts about html