'string' cannot be converted to 'HtmlAgilityPack.HtmlDocument'.

.net c# html-agility-pack

Question

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;

namespace sss
{
    public class Downloader
    {
        WebClient client = new WebClient();

        public HtmlDocument FindMovie(string Title)
        { 
            //This will be implemented later on, it will search movie.
        }

        public HtmlDocument FindKnownMovie(string ID)
        {
            HtmlDocument Page = (HtmlDocument)client.DownloadString(String.Format("http://www.imdb.com/title/{0}/", ID));
        }
    }
}

How can I convert a downloaded string into a legitimate HTML document so that HTMLAgilityPack can parse it?

1
3
6/11/2010 2:42:46 PM

Accepted Answer

This ought to work with v1.4:

HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(string.Format("http://www.imdb.com/title/{0}/", ID));

or

string html = client.DownloadString(String.Format("http://www.imdb.com/title/{0}/", ID));
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
6
6/15/2010 1:45:57 PM

Popular Answer

Based on This outdated document, try this:

string url = String.Format("http://www.imdb.com/title/{0}/", ID);
string content = client.DownloadString(url);
HtmlDocument page = new HtmlDocument();
page.LoadHtml(content);

In general, casting is rarely the proper method of converting between two types, especially while parsing is taking place.



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