Magnus Gladh
Magnus Gladh

Reputation: 1867

Default property for a class when deserialize XML to an object

I have a class that looks like this.

[Serializable]
[DataContract()]
[XmlType(AnonymousType = true)]
public partial class Reference
{
    //-- Constructor
    public Reference() { }

    //-- Properties
    [DataMember]
    [XmlAttribute()]
    public string Type {get;set;}
    [DataMember]
    public string Assignor { get; set; }
    [DataMember]
    public string Identifier { get; set; }
    [DataMember]
    public string System { get; set; }
    [DataMember]
    public string Format { get; set; }
    [DataMember]
    public string Value { get; set; }

Then I got some XML that I want to deserialize into this class, it looks like this.

<References>
  <Reference Type="ShipmentId">SHPROD00324</Reference> 
</References>

And it set the Type-property correct since the [XMLAttribute] but how can I specified that the data that is in the Reference element (SHPROD00324) is placed in my property named Identifier?

What I am looking for is some way of telling the deserializing that if there is any value specified in the Reference element then it should take that value and set it in the identifier property.

Thanks in advance.

Best regards Magnus

Upvotes: 0

Views: 330

Answers (2)

Eric
Eric

Reputation: 401

Try this

[DataMember]
[XMLText]
  public string Identifier { get; set; }

Upvotes: 1

Trevor Pilley
Trevor Pilley

Reputation: 16393

I'm not sure if you can specify this by the attribute values, however your class could implement System.Xml.Serialization.IXmlSerializable and override the ReadXml method to de-serialize its self however it needs to.

Upvotes: 1

Related Questions