Windows应用商店中的HtmlAgilityPack

时间:2014-04-21 03:58:42

标签: c# windows-runtime

所以我在控制台应用程序中有一些工作测试代码,我正在转移到一个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");
        }
    }

2 个答案:

答案 0 :(得分:3)

您可以尝试将WebClient替换为HtmlWeb并使用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);
        }
    }
}

答案 1 :(得分:1)

对于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/