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");
}
}
For XPath, you can use the following link to find an implementation (+source code) for XPath for Windows Phone. The code is easily transferrable to WinRT.
Note: Using LINQ is generally far superior to using XPath. There's one case where that's not true - if your XPaths are coming from a server. In this cases, you can use a solution such as this.
http://socialebola.wordpress.com/2011/07/06/xpath-support-for-the-html-agility-pack-on-windows-phone/
You can try to replace
WebClient
withHtmlWeb
and use HtmlAgilityPack's LINQ API instead of XPath, to make it works in Windows Store apps :