how to XML-Serialize array of an array in FSharp
here's what I'm looking for:
<reports>
<parameters>
<parameter name="srid" type="java.lang.Integer">16533</parameter>
<parameter name="pmid" type="java.lang.Integer">17018</parameter>
<parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00 AM</parameter>
<parameter name="End" type="java.text.SimpleDateFormat">1/31/2011 12:00:00 AM</parameter>
</parameters>
<parameters>
<parameter name="srid" type="java.lang.Integer">16099</parameter>
<parameter name="pmid" type="java.lang.Integer">17018</parameter>
<parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00 AM</parameter>
<parameter name="End" type="java.text.SimpleDateFormat">1/31/2011 12:00:00 AM</parameter>
</parameters>
</reports>
but instead, here's what I'm getting:
<reports>
<parameters>
<parameters>
<parameter name="srid" type="java.lang.Integer">16533</parameter>
<parameter name="pmid" type="jav开发者_JAVA技巧a.lang.Integer">17018</parameter>
<parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00 AM</parameter>
<parameter name="End" type="java.text.SimpleDateFormat">1/31/2011 12:00:00 AM</parameter>
</parameters>
</parameters>
<parameters>
<parameters>
<parameter name="srid" type="java.lang.Integer">16677</parameter>
<parameter name="pmid" type="java.lang.Integer">17018</parameter>
<parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00 AM</parameter>
<parameter name="End" type="java.text.SimpleDateFormat">1/31/2011 12:00:00 AM</parameter>
</parameters>
</parameters>
</reports>
it seems I have an extra <parameters>
tag.
Here's my whole model:
type parameter(paramName, javaType, paramValue) =
let mutable pName = paramName
let mutable pType = javaType
let mutable pValue = paramValue
public new() =
new parameter("","","")
[<XmlAttributeAttribute("name")>]
member this.PName with get() = pName and set v = pName <- v
[<XmlAttributeAttribute("type")>]
member this.PType with get() = pType and set v = pType <- v
[<XmlText>]
member this.PValue with get() = pValue and set v = pValue <- v
type parameters(parameters: parameter array) =
let mutable paramArray = parameters
public new() =
new parameters(Array.empty)
[<XmlArray "parameters">]
member this.ParamArray with get() = paramArray and set v = paramArray <- v
[<XmlRoot("reports")>]
type reports(ps:parameters array) =
let mutable parms = ps
public new() =
new reports(Array.empty)
[<XmlElement("parameters")>]
member this.Ps with get() = parms and set v = parms <- v
ok, so i simplified your types a little to look cleaner:
type parameter(paramName) =
let mutable pName = paramName
public new() =
new parameter("")
[<XmlAttribute("name")>]
member this.PName with get() = pName and set v = pName <- v
type parameters(parameters: parameter array) =
let mutable paramArray = parameters
public new() =
new parameters(Array.empty)
[<XmlElement "parameter">]
member this.ParamArray with get() = paramArray and set v = paramArray <- v
[<XmlRoot("reports")>]
type reports(ps:parameters array) =
let mutable parms = ps
public new() =
new reports(Array.empty)
[<XmlElement("parameters")>]
member this.Ps with get() = parms and set v = parms <- v
and then to serialize these:
let params1 = parameters ([|parameter("a"); parameter("b")|])
let params2 = parameters ([|parameter("c"); parameter("d")|])
let repos = reports ([|params1; params2|])
use writer = System.Xml.XmlWriter.Create @"C:\temp\foo1.xml"
let xs = System.Xml.Serialization.XmlSerializer typeof<reports>
xs.Serialize (writer, repos)
produces:
<reports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<parameters>
<parameter name="a" />
<parameter name="b" />
</parameters>
<parameters>
<parameter name="c" />
<parameter name="d" />
</parameters>
</reports>
hth, alex
精彩评论