Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
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","[email protected]"), 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.

like image 277
Ramy Avatar asked Jun 22 '10 18:06

Ramy


People also ask

What is the correct way of using XML serialization?

As with the CreatePo method, you must first construct an XmlSerializer, passing the type of class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.

What does it mean to serialize XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Is a Namespsace for XML serialization?

Xml. Serialization namespace contains several Attribute classes that can be applied to members of a class. For example, if a class contains a member that will be serialized as an XML element, you can apply the XmlElementAttribute attribute to the member.

Why do we use XmlSerializer class?

XmlSerializer enables you to control how objects are encoded into XML. The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors.


1 Answers

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 of XmlSerializer, 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.

like image 155
Brian Avatar answered Oct 11 '22 23:10

Brian