3

I'm trying to use the FSharp.Data third party library but am getting an error The type 'XmlProvider' is not defined on the XmlProvider class.

namespace KMyMoney

open FSharp.Data

  module Read =

    let xml = File.ReadAllText("KMyMoneySampleFile.xml")
    type KMyMoneySource = XmlProvider<xml>

I'm using NuGet to get the library. Library is 'FSharp.Data 1.1.8'

When I type FSharp.Data. There are four options given: Csv, FreebaseOperators, Json, and RuntimeImplementation.

Am I missing something? I'm relatively new to F#. So, sorry for the simple question. I've looked on GitHub but haven't seen any mention of this problem. I am creating a library in F#.

Jon49
  • 4,444
  • 4
  • 36
  • 73

3 Answers3

5

The parameter between <> is the Sample parameter of the type provider, which has to be a compile time constant. That sample is used to infer the structure of the xml.

Try this instead:

namespace KMyMoney

open FSharp.Data

module Read =

    type KMyMoneySource = XmlProvider<"KMyMoneySampleFile.xml">

and then do

let xml = KMyMoneySource.Load("KMyMoneySampleFile.xml")

or if you're reading the same file you used as the XmlProvider sample parameter, just do this:

let xml = KMyMoneySource.GetSample() 

Note that Type Providers are a feature of F# 3.0, so this only works in VS2012 or upper. If you're using VS2010, you'll just get a bunch of syntax errors.

Gustavo Guerra
  • 5,319
  • 24
  • 42
  • So am I supposed to be using VS 2012 to be able to use FSharp.Data.DesignTime? I'm using VS 2010 on Windows 8. Could that be my problem? When I type in XmlProvider it doesn't recognize it and then gives me an error under the file name <"KMyMoneySampleFile.xml"> - "Unexpected string literal in type arguments" – Jon49 Jul 25 '13 at 21:40
  • 1
    Type providers are a feature of F# 3.0, so you need to be using VS2012 or VS2013. – Gustavo Guerra Jul 26 '13 at 09:54
  • OK, put that in your answer and I'll mark you correct. Thanks for your help! – Jon49 Jul 26 '13 at 16:54
  • I've added that to the answer – Gustavo Guerra Jul 29 '13 at 11:10
3

The data has to be available at compile-time which is achieved by putting a file reference in the angle brackets like this (notice that it is a string literal containing a file path, not a string binding containing the data). You can also achieve this by putting a string literal containing the format in the brackets:

type Stocks = CsvProvider<"../docs/MSFT.csv">

let csv = new CsvProvider<"1,2,3", HasHeaders = false, Schema = "Duration (float<second>),foo,float option">()

See here for more information.

N_A
  • 19,799
  • 4
  • 52
  • 98
  • I tried it and it didn't work. I think I might I found the problem. FSharp.Data.DesignTime is in framework 4.5 and I'm working in 4.0. – Jon49 Jul 14 '13 at 00:54
1

Check out this link. Basically you need to add System.Xml.Linq.dll also as reference to your project.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • I actually put that in my references. I think I might I found the problem. FSharp.Data.DesignTime is in framework 4.5 and I'm working in 4.0. – Jon49 Jul 14 '13 at 00:54
  • FSharp.Data.DesignTime is run inside the compiler only, not at runtime, so it's .NET version has no impact. The .NET version that matters is the one of FSharp.Data.dll, and it's currently at 4.0 – Gustavo Guerra Jul 15 '13 at 12:23