0

XDocument has an overloaded constructor.

XDocument()
XDocument([ParamArray] content: obj[])
XDocument(XDocument)
XDocument(XDeclaration,[ParamArray] content: obj[])

so when I try to create an XDocument

    new XDocument(
        new XDeclaration("1.0", "utf-8", "no"),
        new XElement(XName.Get "Foo"))

it gives me the error

A unique overload for method XDocument...

I ASSUME it can't work out whether to use

XDocument([ParamArray] content: obj[])

or

XDocument(XDeclaration,[ParamArray] content: obj[])

How do I force it to choose the correct one?

MrD at KookerellaLtd
  • 2,412
  • 1
  • 15
  • 17

2 Answers2

1

ah...I've found the answer

https://nbevans.wordpress.com/2015/04/15/super-skinny-xml-document-generation-with-f/

let XDeclaration version encoding standalone = XDeclaration(version, encoding, standalone)
let XLocalName localName namespaceName = XName.Get(localName, namespaceName)
let XName expandedName = XName.Get(expandedName)
let XDocument xdecl content = XDocument(xdecl, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray)
let XComment (value:string) = XComment(value) :> obj
let XElementNS localName namespaceName content = XElement(XLocalName localName namespaceName, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray) :> obj
let XElement expandedName content = XElement(XName expandedName, content |> Seq.map (fun v -> v :> obj) |> Seq.toArray) :> obj
let XAttributeNS localName namespaceName value = XAttribute(XLocalName localName namespaceName, value) :> obj
let XAttribute expandedName value = XAttribute(XName expandedName, value) :> obj

i.e. it looks like I have to explicitly cast my content into an array of objects, so that it can select the correct overload.

MrD at KookerellaLtd
  • 2,412
  • 1
  • 15
  • 17
0

You can either:

  • Box element of the param array: box (new XElement(XName.Get "Foo"))
  • Make the array explicit, and unbox it to obj []: unbox [| new XElement(XName.Get "Foo") |]
torbonde
  • 2,459
  • 1
  • 15
  • 17