One of the common problems encountered when serializing .NET objects to JSON or XML is that the JSON/XML ends up containing a lot of unwanted properties and values. To solve the issue of unwanted JSON/XML, xsd2code has a range of built-in options to fine-tune what gets written from a serialized object.
Step-by-step guide
For XML and/or JSON
- In the Serialization section, activate the option ShouldSerialize (IgnoreEmtyCollection, IgnoreEmptyComplexType, IgnoreEnumType, IgnoreNullable, IgnoreSimpleType, IgnoreEmtytring to true).
- In the JSon Attribute section, enable the option (DefaultValueHandling=Ignore, NullValueHandling=Ignore)
Detailed instructions
- The ShouldSerialize option allows to generate a method named ShouldSerialize [PropertyName] depending on the type. This method is invoked by serializing to each of the inspected properties. If a string type is null or empty the method will return false. Similarly if a numeric type is at its default value, it will be ignored. For a collection with no element, it will be ignored. For a null type, if it has no value, it will be ignored.

Here is a summary table of how the ShouldSerialize [PropertyName] methods will inform the serializer to ignore the property :
Type | Ignored if |
---|
Collection | is null or empty |
ComplexType | is null |
string | is null or empty |
int, float, double | if value is equal to default type value and the value has not been explicitly assigned. |
Nullable | has no value |
Enum | if value is equal to default type value and the value has not been explicitly assigned. |
public partial class Product : EntityBase<Product>
{
#region Private fields
private bool _shouldSerializeqty;
[EditorBrowsable(EditorBrowsableState.Never)]
private string _sKU;
[EditorBrowsable(EditorBrowsableState.Never)]
private string _name;
[EditorBrowsable(EditorBrowsableState.Never)]
private int _qty;
#endregion
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[Newtonsoft.Json.JsonPropertyAttribute("SKU")]
public string SKU
{
get
{
return _sKU;
}
set
{
if ((_sKU == value))
{
return;
}
if (((_sKU == null)
|| (_sKU.Equals(value) != true)))
{
_sKU = value;
OnPropertyChanged("SKU");
}
}
}
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[Newtonsoft.Json.JsonPropertyAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
if ((_name == value))
{
return;
}
if (((_name == null)
|| (_name.Equals(value) != true)))
{
_name = value;
OnPropertyChanged("Name");
}
}
}
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[Newtonsoft.Json.JsonPropertyAttribute("qty")]
public int qty
{
get
{
return _qty;
}
set
{
if ((_qty.Equals(value) != true))
{
_qty = value;
OnPropertyChanged("qty");
}
_shouldSerializeqty = true;
}
}
/// <summary>
/// Test whether qty should be serialized
/// </summary>
public virtual bool ShouldSerializeqty()
{
if (_shouldSerializeqty)
{
return true;
}
return (_qty != default(int));
}
/// <summary>
/// Test whether SKU should be serialized
/// </summary>
public virtual bool ShouldSerializeSKU()
{
return !string.IsNullOrEmpty(SKU);
}
/// <summary>
/// Test whether Name should be serialized
/// </summary>
public virtual bool ShouldSerializeName()
{
return !string.IsNullOrEmpty(Name);
}
}