3

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>.$TextRuntime.GetNonOptionalValue@109-4.Invoke(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.

Kevin
  • 2,281
  • 1
  • 14
  • 16
  • 3
    You probably need to include a sample where this value is missing. – Foole Jul 01 '15 at 07:42
  • 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 Jul 01 '15 at 08:02
  • @kevin Not really. There is a subtle difference in and not existing and the "illegal" (ok that depends on a lot but still). Finer info at http://stackoverflow.com/questions/3760629/whats-the-difference-between-a-element-and-a-element-xsinil-true This then again have implications when inferring stuff from XML... – Helge Rene Urholm Jul 01 '15 at 09:25

1 Answers1

0

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