Imagine you have the below property which should be serialized as an xml attribute if it has value and if not, it should just be ignored from the serialization:
[XmlAttribute("lastUpdated")]
public DateTime? LastUpdated { get; set; }
The solution would be introducing a new property and a new method:
[XmlIgnore]
public DateTime? LastUpdated { get; set; }
[XmlAttribute("lastUpdated")]
public DateTime LastUpdatedSerializable
{
get { return this.LastUpdated.Value; }
set { this.LastUpdated = value; }
}
public bool ShouldSerializeLastUpdatedSerializable()
{
return LastUpdated.HasValue;
}
It's important to know that ShouldSerialize{Property} and {Property}Specified are .NET Conventions to resolve this issue.
I liked to find a better/cleaner/simpler approach but haven't found any yet.
1 comment:
//nullable atribut id
private int? _id { get; set; }
[XmlAttribute("id")]
public int id
{
get { return _id.Value; }
set { _id = value; }
}
public bool ShouldSerializeid()
{
return _id.HasValue;
}
Post a Comment