XmlSerializer和XmlArrayItem

时间:2010-12-02 15:41:20

标签: c# .net f# xmlserializer

Xml如下所示:

<Publisher sequence="1" primaryIndicator="Yes">
  <PublisherID idType="Shortname">ysc</PublisherID>
  <PublisherID idType="xy" encrypted="VlsC1V9K23Leo1BAOk6nxxROZAPKSAny" library="http://xys.abc.com">21503</PublisherID>
  <PublisherName nameType="Legal">xys legal name</PublisherName>
</Publisher>

我的班级被映射为:

type PublisherId() = 
    [<DefaultValue>] val mutable _idType: string ;
    [<DefaultValue>] val mutable _encrypted: string ;
    [<DefaultValue>] val mutable _library: string ;
    [<DefaultValue>] val mutable _value: string ;

    [<XmlAttribute>] member this.idType with get() = this._idType and set(v) = this._idType <- v
    [<XmlAttribute>] member this.encrypted with get() = this._encrypted and set(v) = this._encrypted <- v
    [<XmlAttribute>] member this.library with get() = this._library and set(v) = this._library <- v
    [<XmlTextAttribute>] member this.value with get() = this._value and set(v) = this._value <- v

type Publisher() as this = 
    [<DefaultValue>] val mutable _sequence : int
    [<DefaultValue>] val mutable _primaryIndicator: string ;
    [<DefaultValue>] val mutable _publisherIds : List<PublisherId>

    do
       this._publisherIds <- new List<PublisherId>(); 

    [<XmlAttribute>] member this.sequence with get() = this._sequence and set(v) = this._sequence <- v
    [<XmlAttribute>] member this.primaryIndicator with get() = this._primaryIndicator and set(v) = this._primaryIndicator <- v

    [<XmlArrayAttribute>]
    [<XmlArrayItem(typeof<PublisherId>, ElementName = "PublisherID")>]
    member this.PublisherID with get() = this._publisherIds and set(v) = this._publisherIds <- v

我还修改了有问题的elemetn上的成员属性:

    [<XmlArrayItem(typeof<PublisherId>, ElementName = "PublisherID")>]
    member this.PublisherID with get() = this._publisherIds and set(v) = this._publisherIds <- v

问题在于,它没有填充_publisherIds字段。我尝试切换到一个数组,这也没有帮助。 setter中的断点永远不会被击中,所以我认为注释有问题。

我在以下结构方面取得了成功:

<Publisher sequence="1" primaryIndicator="Yes">
  <PublisherIDs>
     <PublisherID idType="Shortname">ysc</PublisherID>
     <PublisherID idType="xy" encrypted="VlsC1V9K23Leo1BAOk6nxxROZAPKSAny" library="http://xys.abc.com">21503</PublisherID>
   </PublisherIDs>
  <PublisherName nameType="Legal">xys legal name</PublisherName>
</Publisher>

使用类似的属性(尽管在c#中),但是更改XML结构不是选项 - 因为这是来自供应商。

注意:我正在标记为C#,以及该组可能能够帮助注释。如果这是不合适的标签,请删除我的应用程序。

谢谢

1 个答案:

答案 0 :(得分:3)

在这里找到答案:

using XmlArrayItem attribute without XmlArray on Serializable C# class

对于那些跟在家里的人:

[<XmlElement>] member this.PublisherID with get() = this._publisherIds and set(v) = this._publisherIds <- v