Изменить имя тега в XML-сериализации с помощью F # - PullRequest
0 голосов
/ 03 февраля 2011

Вот что я получаю:

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

4 голосов
/ 03 февраля 2011

я думаю, что вы должны украсить свойство Recipients с помощью

[<XmlElement "recipients">]

, сейчас похоже, что вы также декорируете массив с атрибутом массива, у вас может быть ElementName = "receients", где он должен быть "получателем"

вот более полный пример, я вынул другие свойства:

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 голос
/ 03 февраля 2011

Алекс довольно близок, волшебство, которое тебе нужно, это поставить [<XmlArray "reciplients">].

...