Tuesday, December 20, 2016

Use multiple XML samples with F# XML Type Provider

I’m a big fan of the F# type providers. They make my life working with JSON, XML,… a lot easier and less error-prone.

Last week however I had a problem when parsing a .nuspec NuGet file. I used the following NuSpec file as the sample file:

In this sample, you see in the metadata tags that some dependencies are available. However when I tried to load another specfile without any dependencies the F# XML type provider complained that it couldn’t find any dependency elements inside the metadata tag.

So how can I tell the XML type provider that the dependencies are optional? One way to do this is extend the sample file with another metadata tag without dependencies and telling the XML type provider that a list of samples is provided(using  SampleIsList=true).

No comments: