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?
You can add one or more ShouldSerializeXXXXXX()
methods, where XXXXXX
is the name of each property you want to serialize based on a condition.
Eg:
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
}
You can use XmlAttributeOverrides
and overide the XmlIgnore
attribute for your property.
(there is an example in the XmlIgnore
msdn page)
Take a look at this post on SO . Can be helpful for You.
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.