如何在Lightswitch 2013桌面应用程序中使用HTML Agility Pack

时间:2014-02-15 18:20:38

标签: c# visual-studio-lightswitch

我使用Nuget将HTML Agility Pack(HAP)添加到我的解决方案中。我的解决方案使用Silverlight 5.我可以通过“使用HtmlAgilityPack”调用一些函数。但有些功能我不能打电话。我需要通过以下代码行获取URL的内容:

string Url = "http://google.com.vn"; // this line is okay
HtmlWeb web = new HtmlWeb(); // this line is okay
HtmlDocument doc = web.Load(Url); // but this line is highlighted with an error 'Error HtmlAgilityPack.HtmlWeb' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'HtmlAgilityPack.HtmlWeb' could be found (are you missing a using directive or an assembly reference?)'

1 个答案:

答案 0 :(得分:0)

我解决了我的问题

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows;
using Microsoft.LightSwitch.Threading;
using HtmlAgilityPack;

namespace LightSwitchApplication
{
public partial class Table1ItemsListDetail
{
    partial void Method_CanExecute(ref bool result)
    {

        // Write your code here.


    }

    partial void Method_Execute()
    {

            var wc = new HtmlWeb();
            // string url = "http://www.nu.nl/feeds/rss/algemeen.rss";

            wc.LoadAsync("http://google.com");

            wc.LoadCompleted += new EventHandler<HtmlDocumentLoadCompleted>(DownloadCompleted);
            //wc.
        //

    }
    void DownloadCompleted(object sender, HtmlDocumentLoadCompleted e)
    {
        if (e.Error == null)
        {
            HtmlDocument doc = e.Document;
            if (doc != null)
            {
                Dispatchers.Main.BeginInvoke(() =>
                {
                    MessageBox.Show(doc.DocumentNode.Element("html").InnerHtml);

                });
            }
        }
    }

}

}