简体   繁体   中英

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

我不确定是否可以通过属性值指定此名称,但是您的类可以实现System.Xml.Serialization.IXmlSerializable并重写ReadXml方法以反序列化其自身(但需要这样做)。

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM