簡體   English   中英

Windows應用商店中的HtmlAgilityPack

[英]HtmlAgilityPack in windows store app

所以我在控制台應用程序中有一些工作測試代碼,我正在轉移到Windows應用商店應用程序。 不,問題是,我剛剛復制了我在控制台應用程序中的HtmlAgilityPack代碼,現在它不起作用。 我確實有HtmlAgilityPack作為參考...

現在一些HtmlAgilityPack確實有效。 什么是無效的
“使用(var client = new WebClient())”只是通過錯誤“無法找到類型或命名空間名稱'WebClient'(您是否缺少using指令或程序集引用?)”

並且下一部分不起作用的是“foreach(doc.DocumentNode.SelectNodes中的HtmlNode鏈接(”// a [@href]“))”“在selectnodes部分,錯誤”'HtmlAgilityPack.HtmlNode'不包含'SelectNodes'的定義,沒有擴展方法'SelectNodes'接受類型為'HtmlAgilityPack.HtmlNode'的第一個參數'(你是否缺少using指令或匯編引用)“

現在N知道Html Agility Pack依賴.NET來實現XPATH。 而且WinRT不支持XPATH。 現在我的問題是,我將如何使用將在Windows應用商店中運行的內容完成相同的操作?

下面的代碼執行以下操作。 http://www.dubstep.net/track/5436下載html頁面,一旦找到#,就會遍歷它尋找href。 它需要高於它的href並將其作為uri發送。

我已經驗證下面的代碼可以在控制台應用程序中工作。

 using (var client = new WebClient())
        {
            // Download the HTML
            string html = client.DownloadString("http://www.dubstep.net/track/5436");

            // Now feed it to HTML Agility Pack:
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            int i = 0;
            // Now you could query the DOM. For example you could extract
            // all href attributes from all anchors:
            List<string> list = new List<string>();
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                HtmlAttribute href = link.Attributes["href"];
                if (href != null)
                {
                    list.Add(href.Value);
                    i++;
                    if (href.Value == "#")
                    {
                        int t = i - 2;
                        Uri test = new Uri(list[t]);
                        start(test);
                    }
                }
            }
        }

 public static void start(Uri t)
    {
        Uri remoteUri = new Uri("http://soundcloud.com/dubstep/spag-heddy-the-master-vip/download");
        string fileName1 = "t", myStringWebResource = null;

        // Create a new WebClient instance.
        using (WebClient myWebClient = new WebClient())
        {
            myWebClient.DownloadFileCompleted += DownloadCompleted;
            myWebClient.DownloadProgressChanged += myWebClient_DownloadProgressChanged;
            myWebClient.DownloadFileAsync(t, "file.mp3");
        }
    }

您可以嘗試使用HtmlWeb替換WebClient並使用HtmlAgilityPack的LINQ API而不是XPath,以使其在Windows應用商店應用中運行:

//use HAP's HtmlWeb instead of WebClient
var htmlweb = new HtmlWeb();
// load HtmlDocument from web URL
HtmlDocument doc = htmlweb.Load("http://www.dubstep.net/track/5436");


int i = 0;
List<string> list = new List<string>();

//use LINQ API to select all `<a>` having `href` attribute
var links = doc.DocumentNode
               .DescendantsAndSelf("a")
               .Where(o => o.GetAttributeValue("href", null) != null);
foreach (HtmlNode link in links)
{
    HtmlAttribute href = link.Attributes["href"];
    if (href != null)
    {
        list.Add(href.Value);
        i++;
        if (href.Value == "#")
        {
            int t = i - 2;
            Uri test = new Uri(list[t]);
            start(test);
        }
    }
}

對於XPath,您可以使用以下鏈接查找適用於Windows Phone的XPath的實現(+源代碼)。 代碼很容易轉移到WinRT。

注意:使用LINQ通常遠遠優於使用XPath。 有一種情況並非如此 - 如果您的XPath來自服務器。 在這種情況下,您可以使用此類解決方案。

http://socialebola.wordpress.com/2011/07/06/xpath-support-for-the-html-agility-pack-on-windows-phone/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM