F# Serialization of Record types

后端 未结 4 1109
南旧
南旧 2021-02-04 08:42

I know how to serialize in F# using mutable objects, but is there a way to serialize/deserialize using record types using either XmlSerializer or the DataContractSerializer? loo

4条回答
  •  终归单人心
    2021-02-04 09:14

    You can use this series of annotations on the properties of classes to format the XML:

    [XmlRoot("root")]
    [XmlElement("some-element")]
    [XmlAttribute("some-attribute")]
    [XmlArrayAttribute("collections")]
    [XmlArrayItem(typeof(SomeClass), ElementName = "item")]
    

    I use the attributes on my c# classes, but deserialize in F# (c# classes are ina referenced lib).

    in f#:

    use file = new FileStream(filePath, FileMode.Open)
    let serializer= XmlSerializer(typeof)
    let docs = serializer.Deserialize file :?> SomeClass
    

提交回复
热议问题