代码之家  ›  专栏  ›  技术社区  ›  Ramy

带F的XML序列化#

  •  4
  • Ramy  · 技术社区  · 15 年前

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

    //x和y是用来保护无辜的。

    1 回复  |  直到 14 年前
        1
  •  4
  •   Brian    15 年前

    我认为一些F#类型(比如'list')不允许使用XmlSerializer进行序列化(例如,序列化技术需要使用字段设置器或公共默认构造函数的类型或类似的废话)。我认为一些选项可能会立即解除你的封锁,包括

    • 从使用F#列表改为使用.NET数组( record list record [] 等)
    • (可能)使用 DataContractSerializer XmlSerializer ,因为DCS对类型的要求不高(我忘了它是否适用于F#list)

    希望其他更熟悉.NET序列化技术的人能够提供更明确的答案。