3
votes

I have some Xml with one of the nodes as follows:

<code>23</code>

FSharp.Data correctly infers the type of "Code" to be an option int. My problem is when the node is empty as follows:

<code />

I get the following exception

Value is missing at <StartupCode$FSharp-Data>[email protected](String message) in C:\Git\FSharp.Data\src\CommonRuntime\TextRuntime.fs:line 109

Is there any way I can get back Option.None instead of it throwing an exception?

EDIT: For types inferred as option string this works as expected.

1
You probably need to include a sample where this value is missing. - Foole
Spot on. Including an additional sample where Code is empty prevents the exception. I was under the impression that having the type inferred as Option int would be enough. - Kevin
@kevin Not really. There is a subtle difference in <code xsi:nil/> and not existing and the "illegal" <code/> (ok that depends on a lot but still). Finer info at stackoverflow.com/questions/3760629/… This then again have implications when inferring stuff from XML... - Helge Rene Urholm

1 Answers

0
votes

Did you try the SampleIsList one ? Here is a sample working fine.

#r "packages/FSharp.Data/lib/net45/FSharp.Data.dll"

#r "System.Xml.Linq"

open FSharp.Data

type Code = XmlProvider< """<samples><code>123</code><code /></samples>""", SampleIsList=true >

Code.Parse("<code>123</code>") = Some 123 //true
Code.Parse("<code />") = None //true