Reputation: 16322
Suppose i have one customer
class and i will serialize the class to xml
. After serialization we will get xml data but i need some property of customer
class to be serialized on demand based on few condition. Is it possible?
I have no concept how to do it. Can anyone help me with this?
Upvotes: 1
Views: 123
Reputation: 51302
You can add one or more ShouldSerializeXXXXXX()
methods, where XXXXXX
is the name of each property you want to serialize based on a condition.
E.g.:
public class Customer
{
[DefaultValue(null)]
public string SomeInfo { get; set; }
[DefaultValue(null)]
public string SomeOtherInfo { get; set; }
#region Serialization conditions
// should SomeInfo be serialized?
public bool ShouldSerializeSomeInfo()
{
return SomeInfo != null; // serialize if not null
}
// should SomeOtherInfo be serialized?
public bool ShouldSerializeSomeOtherInfo()
{
return SomeOtherInfo != null; // serialize if not null
}
#endregion
}
Upvotes: 2
Reputation: 47968
You can use XmlAttributeOverrides
and overide the XmlIgnore
attribute for your property.
(there is an example in the XmlIgnore
msdn page)
Upvotes: 1