微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用FSharp创建XML属性(而不是元素)?

我需要创建如下所示的 XML

<record id="100000000000000000" type="Message">  
    ...a bunch of xml ...
</record>

相反,使用我正在使用的FSsharp代码,我得到了这个:

<record>
    <type>Message</type>
    <id>118448</id>
    ...a bunch of xml....
</record>

这是我目前正在做的事情:

type record( id:int,sr:sender,recipients: recipient array,atts : attachment array,con : conversation,madeDate : creation) =  
    let mutable id: int = id
    let mutable typ = "Message"
    let mutable creation = madeDate
    let mutable sender  = sr
    let mutable recipients = recipients
    let mutable conversation = con
    let mutable attachments = atts

    public new() =
        record( -1,sender(-1,"Joe","Plumber","Joe@plumber.com"),Array.empty,conversation(),creation())    

    [<XmlElement("type")>] 
    member this.Type with get() = typ and set v = typ <- v

    [<XmlElementAttribute("id")>] 
    member this.Id with get() = id and set v = id <- v

    [<XmlElement("creation")>] 
    member this.Creation with get() = creation and set v = creation <- v

    [<XmlElement("sender")>]
    member this.Sender with get() = sender and set v = sender <- v

    [<XmlArrayAttribute("recipients")>]
    [<XmlArrayItem(typeof<recipient>,ElementName = "recipient")>]
    member this.Recipients with get() = recipients and set v = recipients <- v   

    [<XmlElement("conversation_info")>]
    member this.Conversation with get() = conversation and set v = conversation <- v

    [<XmlArrayAttribute("attachments")>]        
    [<XmlArrayItem(typeof<attachment>,ElementName = "attachment")>]
    member this.Attachments with get() = attachments and set v = attachments <- v

解决方法

我想你想要XmlAttributeAttribute而不是XmlElementAttribute.

注意

[<XmlElement>]

[<XmlElementAttribute>]

是一回事(就像C#一样).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。