使用F#更改XML序列化中的标记名称

时间:2011-02-03 16:28:57

标签: .net f# xml-serialization

这就是我得到的:

<Recipients>
    <recipients>
             <mid_name /> 
             <last_name>Community</last_name> 
             <company>co, inc.</company> 
             <user_id>871</user_id> 
             <email>community@co.com</email> 
     </recipients>
</Recipients>

这应该是这样的。

<recipients>
    <recipient>
             <mid_name /> 
             <last_name>Community</last_name> 
             <company>co, inc.</company> 
             <user_id>871</user_id> 
             <email>community@co.com</email> 
     </recipient>
</recipients>

这就是我的数据模型:

type recipient(userId:int,  first:string, last:string, email: string, company: string) = 
    let mutable first = first
    let mutable mid = "" ;
    let mutable last = last
    let mutable company = company
    let mutable userId = userId
    let mutable email = email

    public new() =
        new recipient(12180,"Joe","Plumber","Joe@plumber.com","tubes tied, inc")

    [<XmlElement("first_name")>] 
    member this.First with get() = first and set v = first <- v

    [<XmlElement("mid_name")>] 
    member this.Mid with get() = mid and set v = mid <- v

    [<XmlElement("last_name")>] 
    member this.Last with get() = last and set v = last <- v

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

    [<XmlElement("user_id")>] 
    member this.UserId with get() = userId and set v = userId <- v

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

而且:

type record( id:int, sr:sender, recipients: recipient array, atts : attachment array, con : conversation, madeDate : creation) =  
    let mutable id: int = id
    let mutable typ = "Idea"
    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())    

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

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

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

    [<XmlElement("interaction_id")>] 
    member this.InteractionId with get() = id and set v = id <- v

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

    //[<XmlArrayAttribute("recipients")>]
    [<XmlArrayItem(typeof<recipient>, ElementName = "recipients")>]
    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

如何更改我的数据模型,以便大多数标记从“收件人”更改为“收件人”,内部标记从“收件人”更改为“收件人”?

2 个答案:

答案 0 :(得分:4)

我认为你应该用

装饰Recipients属性
[<XmlElement "recipients">]

现在看起来你正在使用数组属性来装饰数组 另外,你可能有ElementName =“收件人”,它应该是“收件人”

这里是更完整的例子,我拿出了其他属性:

type recipient (f:string) = 
   let mutable first = f

   public new () = recipient ("")                   

   [<System.Xml.Serialization.XmlElement("first_name")>] 
   member this.First with get() = first and set v = first <- v



   [<System.Xml.Serialization.XmlRoot("recipients")>]
   type Record (recepts: recipient array) =

      let mutable recipients = recepts

      public new () = Record(Array.empty)                 

      [<System.Xml.Serialization.XmlElement("recipient")>]
      member this.Recipients with get() = recipients and set v = recipients <- v   

然后

    let records = [|recipient("a");recipient("b");recipient("c")|]
    let data = Record(records)
    use writer = System.Xml.XmlWriter.Create @"C:\temp\foo.xml"
    let xs = System.Xml.Serialization.XmlSerializer typeof<Record>
    xs.Serialize (writer, data)

产生

<recipients xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<recipient>
    <first_name>a</first_name>
</recipient>
<recipient>
    <first_name>b</first_name>
</recipient>
<recipient>
    <first_name>c</first_name>
</recipient>

答案 1 :(得分:1)

亚历克斯非常接近,你需要的神奇juju是放[<XmlArray "reciplients">]