HtmlAgilityPack在Windows商店应用 [英] HtmlAgilityPack in windows store app
问题描述
所以我不得不在一个控制台应用程序工作的一些测试代码,那我动过到Windows商店应用。现在的问题是,我刚复制,我在我的控制台应用程序,现在它不工作的HtmlAgilityPack代码。我确实有HtmlAgilityPack作为参考...
现在的HtmlAgilityPack的一些不工作。什么是不工作的结果
使用(VAR的客户=新的WebClient())刚刚经历错误的类型或命名空间名称的WebClient'找不到(是否缺少using指令或程序集引用?)
和下一部分不工作为
在doc.DocumentNode.SelectNodes(的foreach(HtmlNode链接//一个[ @href]))的一部分的selectNodes,出现错误'HtmlAgilityPack.HtmlNode'不包含'的SelectNodes的定义,并没有扩展方法'的SelectNodes接受型HtmlAgilityPack.HtmlNode'的第一个参数可能是(是否缺少using指令或程序集引用)
现在ñ知道的Html敏捷性包依赖于.NET的XPath实现。这WinRT的不支持XPATH。现在的问题是,我将如何完成下同的东西,会在Windows商店应用运行?
下面的代码做了以下几点。从下载 http://www.dubstep.net/track/5436 中的HTML页面,通过循环它寻找HREF,一旦它找到一个#。这需要在href它上面和并且将其作为一个URI来启动。
我已经验证,下面的代码不会在控制台应用程序的工作。
使用(VAR的客户=新的WebClient())
{
//下载HTML
字符串的html = client.DownloadString(http://www.dubstep.net/track/5436);
//现在把它交给HTML敏捷性包:
的HTMLDocument DOC =新的HTMLDocument();
doc.LoadHtml(HTML);
INT I = 0;
//现在你可以查询DOM。例如,你可以提取
//从所有锚的所有HREF属性:
名单,LT;字符串>名单=新名单,LT;串>();
的foreach(在doc.DocumentNode.SelectNodes(HtmlNode链接//一个[@href]))
{
HtmlAttribute HREF = link.Attributes [HREF];
如果(HREF!= NULL)
{
list.Add(href.Value);
I ++;
如果(href.Value ==#)
{
INT T = I - 2;
乌里试验=新的URI(列表[T]);
开始(试验);
}
}
}
}
公共静态无效的开始(URI T)
{
乌里remoteUri =新的URI (http://soundcloud.com/dubstep/spag-heddy-the-master-vip/download);
串FILENAME1 =T,myStringWebResource = NULL;
//创建一个新的WebClient实例。
使用(Web客户端myWebClient =新的WebClient())
{
myWebClient.DownloadFileCompleted + = DownloadCompleted;
myWebClient.DownloadProgressChanged + = myWebClient_DownloadProgressChanged;
myWebClient.DownloadFileAsync(T,file.mp3);
}
}
您可以尝试更换 Web客户端
与 HtmlWeb
和使用HtmlAgilityPack的LINQ API,而不是XPath中,使其工作在Windows应用商店中的应用程序
//使用HAP的htmlWeb,而不是Web客户端
变种htmlweb =新htmlWeb();从网页URL
的HTMLDocument DOC = htmlweb.Load(http://www.dubstep.net/track/5436)
//负荷的HTMLDocument;
INT I = 0;
名单,LT;字符串>名单=新名单,LT;串>();
//使用LINQ API来选择所有`< A>``有属性href`
VAR链接= doc.DocumentNode
.DescendantsAndSelf(A)
。凡(O => o.GetAttributeValue(的href,NULL)!= NULL);
的foreach(在链接HtmlNode链接)
{
HtmlAttribute HREF = link.Attributes [HREF];
如果(HREF!= NULL)
{
list.Add(href.Value);
I ++;
如果(href.Value ==#)
{
INT T = I - 2;
乌里试验=新的URI(列表[T]);
开始(试验);
}
}
}
So I had some working test code in a console app, that I am moving over to a windows store app. NoW the problem is, ive just copied over the HtmlAgilityPack code that I had in my console app and now it doesnt work. I do have HtmlAgilityPack as a reference...
Now some of the HtmlAgilityPack does work. what is not working is
"using (var client = new WebClient())" just through the error "The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)"
and the next part that does not work is " foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))" at the selectnodes part, with the error "'HtmlAgilityPack.HtmlNode' does not contain a definition for 'SelectNodes' and no extension method 'SelectNodes' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference)"
Now N know that Html Agility Pack relies on .NET for the XPATH implementation. And that WinRT doesn't support XPATH. Now my question is, how would I accomplish the same below with something that will run in a windows store app?
The code below does the the following. Downloads the html page from http://www.dubstep.net/track/5436, loops through it looking for href, once it finds a #. It takes the href above it and and sends it as a uri to start.
i have verified that the code below does work in a console application.
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");
}
}
You can try to replace WebClient
with HtmlWeb
and use HtmlAgilityPack's LINQ API instead of XPath, to make it works in Windows Store apps :
//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);
}
}
}
这篇关于HtmlAgilityPack在Windows商店应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!