Ramy
Ramy

Reputation: 21271

Change tag name in XML serialization with F#

This is what I'm getting:

<Recipients>
    <recipients>
             <mid_name /> 
             <last_name>Community</last_name> 
             <company>co, inc.</company> 
             <user_id>871</user_id> 
             <email>[email protected]</email> 
     </recipients>
</Recipients>

This is how it should be.

<recipients>
    <recipient>
             <mid_name /> 
             <last_name>Community</last_name> 
             <company>co, inc.</company> 
             <user_id>871</user_id> 
             <email>[email protected]</email> 
     </recipient>
</recipients>

This is what my Data model looks like:

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","[email protected]","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

And this:

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","[email protected]"), 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

How can I change my Data Model so the outter most tag changes from "Recipients" to "recipients" and the inner tag is changed from "recipients" to "recipient"?

Upvotes: 0

Views: 349

Answers (2)

Alex
Alex

Reputation: 2382

i think you should decorate Recipients property with

[<XmlElement "recipients">]

right now it looks like you are decorating the array with an array attribute also, you may have ElementName = "recipients" where it should be "recipient"

here is more complete example, i took out other properties:

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   

then

    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)

produces:

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

Upvotes: 4

Wyatt Barnett
Wyatt Barnett

Reputation: 15663

Alex is pretty close, the magic juju you need is to put the [<XmlArray "reciplients">].

Upvotes: 1

Related Questions