2011-02-08 30 views
3

如何将XML序列化数组的数组这里就是我在寻找:在FSharp

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

这是否对您有帮助? http://stackoverflow.com/questions/2928108/how-to-serialize-this-xml-in-net-array – Brian 2011-02-08 20:25:08

+0

我不相信它。检查我的更新。我用第二组参数进行了澄清。 – Ramy 2011-02-08 20:32:06

回答

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