HtmlDocument和WP8的问题

时间:2014-02-27 20:14:15

标签: c# parsing windows-phone-8 html-agility-pack dom

我想了解HTMLAgilityPack实际上是如何运作的,我在这个网站上找到了一个指南,http://www.tareqateik.com/html-agility-pack%E2%80%93windows-phone-8#.Uw-TcbG8_q4

我遇到HtmlDocument的问题,因为Visual Studio 2013报告错误:“找不到类型或命名空间名称'HtmlDocument'(您是否缺少using指令或程序集引用?)”

这是我正在处理的完整代码,目前:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using FedoraCoin.Resources;
using System.Net.Http;
using System.Windows.Controls.

namespace FedoraCoin
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string htmlPage = "";
            using (var client = new HttpClient())
            {
                htmlPage = await client.GetStringAsync("http://www.imdb.com/movies-in-theaters/");
            }

            HTMLDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(htmlPage);

            List<Movie> movies = new List<Movie>();
            foreach (var div in htmlDocument.DocumentNode.SelectNodes("//div[starts-with(@class, 'list_item')]"))
            {
                Movie newMovie = new Movie();
                newMovie.Cover = div.SelectSingleNode(".//div[@class='image']//img").Attributes["src"].Value;
                newMovie.Title = div.SelectSingleNode(".//h4[@itemprop='name']").InnerText.Trim();
                newMovie.Summary = div.SelectSingleNode(".//div[@class='outline']").InnerText.Trim();
                movies.Add(newMovie);
            }
            lstMovies.ItemsSource = movies;
        }


    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

您需要将库(HTMLAgilityPack)添加到引用中,然后使用文件顶部的“using”关键字来使用它。