XMLSerializer选项字段

时间:2016-05-06 08:33:13

标签: serialization f#

以下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>

为什么“可选”元素为空?

0 个答案:

没有答案