开发者

XmlSerializer and XmlArrayItem

开发者 https://www.devze.com 2023-01-28 05:11 出处:网络
The Xml looks like the following: <Publisher sequence=\"1\" primaryIndicator=\"Yes\"> <PublisherID idType=\"Shortname\">ysc</PublisherID>

The Xml looks like the following:

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

And my class is mapped as:

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 = 
    [<DefaultV开发者_JS百科alue>] 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

and I also modified the member attributes on the elemetn in question:

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

The problem is that, it is not populating the _publisherIds fields. I tried switching to an array and that didn't help either. A breakpoint in the setter never gets hit, so I am thinking there is something wrong with the annotations.

I have had success with structures like:

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

using similar attributes (albeit in c#), however changing the XML structure is not ann option - as this is comming from a vendor.

Note: I am tagging as C# as well as that group may be able to help with the annotations. Please remove with my appologies, if that is an inappropriate tag.

Thank you


Found the answer here:

using XmlArrayItem attribute without XmlArray on Serializable C# class

For those following along at home:

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

精彩评论

暂无评论...
验证码 换一张
取 消