如何在FSharp.Data.XmlProvider中使用保留字?

时间:2019-08-23 11:53:35

标签: xml f# type-providers f#-data fsharp.data.typeproviders

我正在使用FSharp.Data.XmlProvider。我的xml包含一个<load>节点。类型提供程序已经具有用于加载实例的静态Load成员。我寻找了有关如何使XmlProvider正确解释保留字的文档,但没有发现任何东西。有人可以提供有关如何在类型提供程序中生成我的<load>节点的见解吗?也许可以告诉XmlProvider我在xml中将保留字作为节点的构造函数参数?

代码示例:

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 个答案:

答案 0 :(得分:3)

您使用new关键字来解决正确的重载:

[<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) )

还要注意,在示例中loads可以包含多个load项,LoadXml.Root构造函数采用Load实例数组而不是单个实例:

[<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); ... |]