ShouldSerialize - Conditional XML Serialization using C# with example
In case you are serializing your models to generate XML content, you will often encounter empty nodes and elements in the XML which appears when the entity property is not filled or the list is empty. Lets take an example of Employee model class as below: using System.Collections.Generic; using System.Xml.Serialization; public class Employee { public Employee() { this.Skills = new List<string>(); } [XmlElement("FirstName")] public string FirstName { get; set; } [XmlElement("LastName")] public string LastName { get; set; } [XmlArray("Skills")] [XmlArrayItem("Skill")] public List<string> Skills { get; set; } } When you generate XML for the above model by assigning FirstName and LastName only keeping Skills empty: var employeeDetail = new Employee() { FirstName = "Mister", LastName = "XYZ" }; var employeeDetailXml = SerializeXml(employeeDetail); you will get XML content something like this: <?xml version="1.0" » Read more