[英]Change tag name in XML serialization with F#
这就是我得到的:
<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
如何更改数据模型,使最外面的标签从“收件人”更改为“收件人”,而内部标签从“收件人”更改为“收件人”?
我认为您应该用
[<XmlElement "recipients">]
现在看来,您也要使用array属性来修饰数组,您可能在ElementName =“ recipients”的地方应该是“ recipient”
这是更完整的示例,我删除了其他属性:
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>
亚历克斯非常接近,您需要的神奇的法宝是放入[<XmlArray "reciplients">]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.