Q:

F# XML Data parsing a ''subtype''

So I'm using the FSharp.Data library and I have a generated type as such:
<ArrayOfCar>
  <Car>
    <LicenseNumber>000</LicenseNumber>
    <ProductionYear>2014</ProductionYear>
    <Type>Audi</Type>
  </Car>
  <Car>
    <LicenseNumber>999</LicenseNumber>
    <ProductionYear>2012</ProductionYear>
    <Type>BMW</Type>
  </Car>
</ArrayOfCar>

Now I have a question, as it seems the F# compiler will generate two types from this, ArrayOfCars and Car. 
Now I'm calling an API that will return either the example above for a collection of cars and also a single Car with Car then being the root element. However I can only parse ArrayOfCars using ArrayOfCars.Parse there is no Car.Parse as far as I can see.
Is there something I'm missing here or must I then generate a seperate type for Car to handle when it is the root element?

A:

This is a very good question! This does not quite work in the current version of F# Data, but I consider that a bug, so I submitted a pull request which fixes this. The solution below uses the fixed version of F# Data, so you might need to build it from source.
You can pass multiple samples to the XML provider using the IsSampleList=true parameter. Then you can specify that all elements with the same name should be inferred as the same type using Global=true:
type C = XmlProvider<"""
  <Samples> 
    <ArrayOfCar>
      <Car><Type>Audi</Type></Car>
      <Car><Type>BMW</Type></Car>
    </ArrayOfCar>
    <Car><Type>Trabant</Type></Car>
  </Samples>""", SampleIsList=true, Global=true>

With these parameters (and the fix), F# Data now treats Car as the same type in both of these samples, so all you have to do is to read either the Car node with a single car or ArrayOfCar with multiple cars: 
let readCars str = 
  let doc = C.Parse(str)
  match doc.Car, doc.ArrayOfCar with
  | Some car, _ -> [| car |]
  | _, Some cars -> cars.Cars

This function can now read both formats:
readCars "<Car><Type>Audi</Type></Car>"
|> Seq.iter (fun c -> printfn "%s" c.Type)
readCars "<ArrayOfCar><Car><Type>Audi</Type></Car><Car><Type>BMW</Type></Car></ArrayOfCar>"
|> Seq.iter (fun c -> printfn "%s" c.Type)

