代码之家  ›  专栏  ›  技术社区  ›  Ramy

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

  •  5
  • Ramy  · 技术社区  · 14 年前

    我需要创建如下所示的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, 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
    
    1 回复  |  直到 14 年前
        1
  •  6
  •   Brian    14 年前

    我想你想要 XmlAttributeAttribute 而不是 XmlElementAttribute .

    请注意

    [<XmlElement>]
    

    [<XmlElementAttribute>]
    

    都是一样的(就像C#)。