以下f#脚本未序列化选项字段:
open System.Xml.Serialization
[<CLIMutable>]
type Repo = {
[<XmlElement("mandatory")>] mandatory : string
[<XmlElement("optional", IsNullable=true)>] optional : string option
}
with
static member serialize data (filename: string) =
let writer = System.Xml.Serialization.XmlSerializer(data.GetType())
use file = new System.IO.StreamWriter(filename)
writer.Serialize(file, data)
let x = { mandatory = "insert text"; optional = Some("optional text") }
Repo.serialize x @"d:\tmp\prova.xml"
结果xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<Repo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<mandatory>insert text</mandatory>
<optional />
</Repo>
为什么“可选”元素为空?