【发布时间】:2014-04-21 03:58:41
【问题描述】:
所以我在控制台应用程序中有一些工作测试代码,我将转移到 Windows 商店应用程序。现在的问题是,我刚刚复制了我在控制台应用程序中的 HtmlAgilityPack 代码,现在它不起作用。我确实有 HtmlAgilityPack 作为参考...
现在一些 HtmlAgilityPack 可以工作了。什么不工作是
“using (var client = new WebClient())”只是通过错误“找不到类型或命名空间名称'WebClient'(您是否缺少 using 指令或程序集引用?)”
下一个不起作用的部分是 "foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))" 在 selectnodes 部分,错误"'HtmlAgilityPack.HtmlNode'不包含'SelectNodes'的定义并且没有扩展可以找到接受“HtmlAgilityPack.HtmlNode”类型的第一个参数的方法“SelectNodes”(您是否缺少 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");
}
}
【问题讨论】:
标签: c# windows-runtime