因此,我在控制台应用程序中有一些有效的测试代码,现在我将其移至Windows应用商店应用程序.现在的问题是,我只复制了我在控制台应用程序中拥有的HtmlAgilityPack代码,但现在不起作用.我确实有HtmlAgilityPack作为参考…...

因此,我在控制台应用程序中有一些有效的测试代码,现在我将其移至Windows应用商店应用程序.现在的问题是,我只复制了我在控制台应用程序中拥有的HtmlAgilityPack代码,但现在不起作用.我确实有HtmlAgilityPack作为参考…
现在,某些HtmlAgilityPack确实可以工作.什么是行不通的
“仅通过错误“找不到类型或名称空间名称’WebClient'(您是否缺少using指令或程序集引用?)”的“使用(var client = new WebClient())””
下一部分不起作用的是
selectnodes部分上的“ foreach(doc.DocumentNode.SelectNodes(“ // a [@href]”))中的HtmlNode链接”,错误为“’HtmlAgilityPack.HtmlNode’,其中不包含’SelectNodes’的定义,并且没有扩展名可以找到方法’SelectNodes’接受类型为’HtmlAgilityPack.HtmlNode’的第一个参数(您是否缺少using指令或程序集引用)”
现在N知道Html Agility Pack依赖于.NET的XPATH实现.而且WinRT不支持XPATH.现在我的问题是,我将如何在Windows Store应用程序中运行以下操作来完成以下任务?
下面的代码执行以下操作.一旦找到#,便从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");
}
}
解决方法:
您可以尝试将WebClient替换为HtmlWeb,并使用HtmlAgilityPack的LINQ API而不是XPath,以使其在Windows Store应用中正常工作:
//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);
}
}
}
本文标题为:C#-Windows Store应用中的HtmlAgilityPack


基础教程推荐
- 通过history解决ajax不支持前进/后退/刷新的问题 2023-02-13
- vue中哪些数组方法不是响应式的 2023-10-08
- JavaScript操作元素教你改变页面内容样式 2023-08-12
- Ajax 跨域如何实现 2022-12-28
- 对hao123进行重构 2022-11-06
- Ajax学习全套(最全最经典) 2023-01-20
- TypeScript接口介绍 2023-08-08
- VUE——组件(四)组件的高级用法 2023-10-08
- JQuery Ajax请求拦截操作 2022-09-08
- 使用flutter创建可移动的stack小部件功能 2023-08-08