XML Serialization with F#
I'm a total F# n00b, so I hope i give you enough information. I created a class called record. I create several instances of this class with data from our database. I then add each record to a list of records. i want to make an xml document with those records.
//this is the record data type i created. I also created a sender and recipient data
//type but those are probably not neccessary to understand the issue
ty开发者_开发百科pe record(id:int, sender:sender, ?recipients: recipient list ) =
let mutable id: int = id
let mutable typ = "Message"
let mutable creation = creation()
let mutable sender = sender
let mutable recipients = recipients
[<XmlIgnore()>]
[<XmlArrayAttribute("recipients")>]
[<XmlArrayItem(typeof<recipient>, ElementName = "recipient")>]
member this.Recipients with get() = recipients and set v = recipients <- v
public new() =
record(12180, sender(12180,"Joe","Plumber","Joe@plumber.com"), list.Empty)
[<XmlElement("id")>]
member this.Id with get() = id and set v = id <- v
[<XmlElement("creation")>]
member this.Creation with get() = creation and set v = creation <- v
[<XmlElement("sender")>]
member this.Sender with get() = sender and set v = sender <- v
//i later call this:
let xmlSerializer = XmlSerializer(typeof<record list>)
I then get this error message at runtime:
There was an error reflecting type 'Microsoft.FSharp.Collections.FSharpList`1[XXXX.Compliance.YYYYY.record]'. //x's and y's used to protect the innocent.
Caveat: I am not sure.
I think that some F# types (like 'list') do not admit serialization with the XmlSerializer (e.g. that serialization technology requires types with field setters or public default constructors or some such nonsense). I think some options that might immediately unblock you include
- change from using F# lists to using .NET arrays (
record list
->record []
, etc) - (possibly) use the
DataContractSerializer
instead ofXmlSerializer
, as the DCS does not require as much from the type (I forget if it works with F# lists or not) - probably something else I have forgotten
Hopefully someone else more immediately familiar with .NET serialization technologies can provide a more definitive answer.
精彩评论