Vagif Abilov
Vagif Abilov

Reputation: 9991

Is it possible to override F# Xml type provider-generated type definition?

I am using F# XML type provider to parse a set of XML files, and it gets confused interpreting an element that contains a country code set to "no" as a boolean. Of course I can roll up my own XML sample with different values but I wonder if an XML type provider can be tweaked to adjust certain type definitions.

An alternative option would be to load types from XSD schema but it doesn't look like XML type provider support this, does it?

Upvotes: 2

Views: 320

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243106

You can use the InferTypesFromValues parameter to tell the type provider that it should (or should not) attempt to infer the type of primitives from the actual values in the sample. For example:

type T1 = XmlProvider<""" <sample code="no" /> """, InferTypesFromValues=true>
T1.GetSample().Code // When enabled, 'Code' is Boolean

type T2 = XmlProvider<""" <sample code="no" /> """, InferTypesFromValues=false>
T2.GetSample().Code // When disabled, 'Code' is just a string

The default value for this is true. Disabling this for XML provider will essentially mean that all values will be returned as strings. For JSON, this is a bit less dramatic - because JSON distinguishes values like 123 (number) and "123" (string).

As you mentioned, XSD is not currently supported, but it is one of the features that we'd certainly like to add (and I don't even need to mention that pull requests are more than welcome :-)).

Upvotes: 3

Related Questions