1

I am serializing an object into XML.

consider class

public class EmpInfo
{
    public string Code;
    public string FirstName;
    public string LastName;
    public string Designation;
    public int Salary;
}

If I don't provide salary then it is by default set as 0 I don't want default values to be set.

In case of Designation, if I don't provide then value is automatically NULL. That tag is not included in XML.

How should I omit the Salary tag if Salary is not provided?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1120418
  • 261
  • 3
  • 8
  • 18
  • 3
    Try nullable int. `public int? Salary;` – I4V Jul 18 '13 at 13:26
  • 1
    One way would be to implement [IXmlSerializable](http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx). Another method might be to have the `int Salary` be ignored in the XML _always_ and have a `string SalarySerialized` wrapper which does conversion. If the value is `0` it returns `null`, any other number it returns the string representation. EDIT: Oh fine l4V, give a better suggestion why don't you! – Chris Sinclair Jul 18 '13 at 13:26
  • Regarding the nullable case, it seems that it will still include the tag when `null`; it outputs an element "" – Chris Sinclair Jul 18 '13 at 13:29
  • 1
    I think this is the answer you are looking for : http://stackoverflow.com/questions/244953/serialize-a-nullable-int/246359#246359 – Mechanical Object Jul 18 '13 at 13:49
  • See example [this][1], is basic example or [this][2]. [1]: http://stackoverflow.com/questions/4146804/xml-serialization-c-sharp [2]: http://stackoverflow.com/questions/8722126/c-sharp-xml-serialization-deserialization – Andrei Chitu Jul 18 '13 at 14:17

0 Answers0