I\'m serializing a class like this
public MyClass
{
public int? a { get; set; }
public int? b { get; set; }
public int? c { get; set; }
}
If you make the class you want to serialise implement IXmlSerializable, you can use the following writer. Note, you will need to implement a reader, but thats not too hard.
public void WriteXml(XmlWriter writer)
{
foreach (var p in GetType().GetProperties())
{
if (p.GetCustomAttributes(typeof(XmlIgnoreAttribute), false).Any())
continue;
var value = p.GetValue(this, null);
if (value != null)
{
writer.WriteStartElement(p.Name);
writer.WriteValue(value);
writer.WriteEndElement();
}
}
}