How to use reserved words in FSharp.Data.XmlProvider?

92 Views Asked by At

I am using FSharp.Data.XmlProvider. My xml contains a <load> node. The type provider already has a static Load member used to load an instance. I looked for documentation regarding how to have the XmlProvider properly interpret reserved words but have not found anything. Can someone provide insight on how to generate my <load> node in the type provider? Perhaps a constructor parameter that can tell XmlProvider that I have reserved words as nodes in my xml?

Code sample:

module LoadModule =
  [<Literal>]
  let loadSample = """
    <root>
      <loads>
        <load loadid="1" value="12345" />
      </loads>
    </root>"""
  type LoadXml = XmlProvider<loadSample>
  let loadXml = LoadXml.Load("")  // This is the static member Load.  LoadXml does not contain a `Load` member for my node
  let x = loadXml.Load // The instance does contain my Load member, 
                       // but I need to construct a `Load []` using a static `Load` member 
                       // because I need to set the values programmatically.  
                       // Instance members are not settable.
1

There are 1 best solutions below

2
On

You use the new keyword to resolve to the correct overload:

[<Literal>]
let loadSample = """
  <root>
    <loads>
      <load loadid="1" value="12345" />
    </loads>
  </root>"""
type LoadXml = XmlProvider<loadSample>
let loadXml = LoadXml.Parse("...")
let x = LoadXml.Root (new LoadXml.Load(loadid=1,value=12345) )

Also note that indicating in the sample that loads can contain multiple load-items, the LoadXml.Root constructor takes an array of Load instances instead of a single instance:

[<Literal>]
let loadSample = """
  <root>
    <loads>
      <load loadid="1" value="12345" />
      <load loadid="2" value="23456" />
    </loads>
  </root>"""
type LoadXml = XmlProvider<loadSample>
let loadXml = LoadXml.Parse("...")
let x = LoadXml.Root [| new LoadXml.Load(loadid=1,value=12345); ... |]