Serializing XML with F #

I am full F # n00b, so I hope that I will give you enough information. I created a class called record. I am creating several instances of this class with data from our database. Then I add each entry to the list of entries. I want to make an XML document with these entries.

//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 type 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>) 

Then I get this runtime error message:

An error has occurred reflecting the type "Microsoft.FSharp.Collections.FSharpList`1 [XXXX.Compliance.YYYYY.record]". // x and y are used to protect the innocent.

+4
source share
1 answer

Caution: I'm not sure.

I think that some types of F # (for example, a "list") do not allow serialization using the XmlSerializer (for example, serialization technology requires types with field installers or default constructors or some such nonsense). I think some options that can immediately unlock you include

  • change the use of F # lists to use .NET arrays ( record listrecord [] , etc.)
  • (maybe) use a DataContractSerializer instead of an XmlSerializer , since DCS doesn't require as much of a type (I forget if it works with F # lists or not)
  • maybe something else that i forgot

Hopefully someone even more familiar with .NET serialization technologies can give a more definitive answer.

+3
source

Source: https://habr.com/ru/post/1313554/


All Articles