2010-06-22 31 views
4

我是一個F#n00b總數,所以我希望我給你足夠的信息。我創建了一個名爲record的類。我用數據庫中的數據創建了這個類的幾個實例。然後我將每條記錄添加到記錄列表中。我想用這些記錄製作一個xml文檔。用F#編碼的XML序列號

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

我然後在運行時得到這個錯誤消息:

有反射型 'Microsoft.FSharp.Collections.FSharpList`1 [XXXX.Compliance.YYYYY.record]' 的誤差。 // x's和y用於保護無辜者。

回答

3

警告:我不確定。我認爲一些F#類型(如'list')不允許使用XmlSerializer進行序列化(例如,該序列化技術需要具有字段設置器或公共默認構造器的類型或一些這樣的廢話)。我認爲,一些可能會立即解除選項,你包括

  • 變化,從使用F#列出使用.NET數組(record list - >record []等)
  • (可能)使用DataContractSerializer代替XmlSerializer,爲DCS不需要從類型之多(我忘了,如果它與F#列出工作與否)
  • 可能是別的東西,我已經忘記了

希望別人更多很快熟悉.NET serializa灰色技術可以提供更明確的答案。

+0

我認爲你是對的。我改變了所有的數組而不是列表。似乎已經做到了。 – Ramy 2010-06-23 18:33:01