4

I'm trying to get HtmlAgilityPack to work with Windows 8 Metro Apps (Windows Store Apps). I've successfully written out all the code I need in a Windows Console App (C#) and it works perfectly for parsing the HTML I need and returning me the required string I need.

// Create a new HtmlDocument and load the incoming string
        HtmlDocument menu = new HtmlDocument();
        menu.OptionUseIdAttribute = true;
        menu.LoadHtml(response);

        HtmlNode nameToRemove = menu.DocumentNode.SelectSingleNode("//*[@id=\"maincontent_0_contentplaceholder_0_lblHall\"]");

My problem is with the DocumentNode.SelectSingleNode call. I'm getting the following error:

Error 2 'HtmlAgilityPack.HtmlNode' does not contain a definition for 'SelectSingleNode' and no extension method 'SelectSingleNode' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)

I'm confirmed that I have all of the references setup the exact same way I did in the Console Application but am unable to get this to work. According to the HtmlAgilityPack twitter account, support for Windows 8 Metro/Windows Phone 8 was added in version 1.4.5. I'm double checked my NuGet Package Manager and I have 1.4.6 installed.

Is there something special that I need to do to select a node by XPath in an HtmlDocument in a Windows 8 App? Any suggestions would be highly appreciated.

Thanks!

Edit: Can anyone help me get the same results with a Linq query then. I'm not sure how I would go about it.

Rohan Kapoor
  • 55
  • 1
  • 8
  • Please check [Metro version and SelectNodes](https://htmlagilitypack.codeplex.com/discussions/359358) & [HtmlAgilityPack and windows 8 winRT](http://stackoverflow.com/questions/12829137/htmlagilitypack-and-windows-8-winrt) – Farhan Ghumra Apr 11 '13 at 06:04
  • Can anyone help me get the same results with a Linq query then. I'm not sure how I would go about it. – Rohan Kapoor Apr 11 '13 at 06:08

1 Answers1

10

The Html Agility Pack relies on .NET for the XPATH implementation. Unfortunately, WinRT doesn't support XPATH, so you don't have anything related to XPATH in Html Agility Pack for WinRT.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 1
    Its odd this answer is correct if HtmlAgilityPack claims to support Windows 8 Metro ( i.e. the .NET WinRT Profile ) this should be possible. My guess this project doesn't actually support Windows 8 ( Windows Store ) applications despite the claim it does. I actually don't see this claim on the projects website.... – Security Hound Apr 11 '13 at 16:11
  • 1
    It does supports WinRT, but you'll have to use other means than XPATH, things such as Linq, Descendants, etc... – Simon Mourier Apr 11 '13 at 16:47
  • I think I had a brain fart, I was reading WinRT as Windows RT, instead of WinRT .NET profile ( even though I was thinking it ). – Security Hound Apr 11 '13 at 16:50
  • Can you walk me through writing my query with Linq and Descendants? – Rohan Kapoor Apr 11 '13 at 17:38
  • Well at least I know I haven't cocked it up myself. Using LINQ instead is hardly a burden. Simon, I can't thank you enough for lifting the diabolical burden of HTML parsing. +1 for writing HAP. In case no-one has bothered to tell you, Nuget gets it wrong and you have to remove the reference to sl4 and use the sl3 binary instead. – Peter Wone Dec 18 '13 at 11:13