如何在FSharp中XML-Serialize数组的数组

时间:2011-02-08 20:08:17

标签: f# xml-serialization

这就是我要找的东西:

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

但相反,这就是我得到的:

<reports>
    <parameters>
      <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>
    <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>

似乎我有一个额外的<parameters>标签。

这是我的整个模型:

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

1 个答案:

答案 0 :(得分:1)

好的,所以我简化了你的类型,看起来更干净:

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

然后序列化这些:

    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)

产生:

<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