Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fsharp.Data, HtmlDocument has no extension Descendants

I'm trying out f# on a mac and am running into a problem with a very simple script. I'm following this tutorial, however it breaks at results.Descendants ["a"].

Interestingly, VS recognises .Descendants as a valid method of results, but at runtime (trying just Descendants with no params) I get:

error CS1061: 'HtmlDocument' does not contain a definition for 'Descendants' and no 
extension method 'Descendants' accepting a first argument of type 'HtmlDocument' could
be found (are you missing a using directive or an assembly reference?)

It seems as if FSharp.Data is only partially imported or something. Interestingly, HtmlDocument works, but extensions don't.

Any ideas how to fix this?

Edit: code

// Learn more about F# at http://fsharp.org

open System
open FSharp.Data

[<EntryPoint>]
let main argv =
    let results = HtmlDocument.Load("http://www.google.co.uk/search?q=FSharp.Data")
    let links = 
        results.Descendants "a"
        |> Seq.choose (fun x -> 
               x.TryGetAttribute("href")
               |> Option.map (fun a -> x.InnerText(), a.Value())
        )
    0 // return an integer exit code

Edit2: I downloaded FSharp.Data with nuget manually and I can run the below script in fsharpi. I don't know what's the difference between this and whatever visual studio does?

#r "./FSharp.Data.2.4.6/lib/net45/FSharp.Data.dll" 
open FSharp.Data

let results = HtmlDocument.Load("http://www.google.co.uk/search?q=FSharp.Data")
let d= results.Descendants "a"

Edit 3: The differences between running in fsharpi and vs:

  • fsharpi is using mono, while VS is using dotnet (which is .net core 2)
  • nuget downloaded FSharp.Core 4.0.0.1, while VS is using 4.3.4

I can't seem to force VS to use FSharp.Core 4.0.0.1. Even if I add specific version, it still uses 4.3.4

like image 413
dkone Avatar asked Feb 06 '26 02:02

dkone


1 Answers

It seems the problem is in FCore.Sharp Visual Studio is using by default. To use specific version, it's not enough to add it through nuget, but one must add

<FSharpCoreImplicitPackageVersion>4.4.1.18</FSharpCoreImplicitPackageVersion>

to the project config. With that it seems the code works, but debugger (watch window) and immediate window still fail to recognise Descendants method. I don't know how that's even possible. I assume some kind of bug in VS.

Another option seems to be using FSharp.Data 3 beta (with the same watch/immediate problem)

like image 189
dkone Avatar answered Feb 08 '26 23:02

dkone