无法将'string'类型转换为'HtmlAgilityPack.HtmlDocument'?

.net c# html-agility-pack

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));
        }
    }
}

如何将下载的字符串转换为有效的HtmlDocument,以便我可以使用HTMLAgilityPack解析它?

一般承认的答案

这适用于v1.4:

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

要么

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

热门答案

试试这个(基于这个相当古老的文档 ):

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

基本上,转换很少是在两种类型之间进行转换的正确方法 - 特别是在进行解析时。



Related

许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow