Search code examples
.netxml-serializationxmlserializer

How do I stop an empty tag from being emitted by XmlSerializer?


I have an object like this,

public class UserObj
{
    public string First {get; set;}
    public string Last  {get; set;}
    public addr Address {get; set;}

}

public class addr
{
    public street {get; set;}
    public town   {get; set;}
}

Now when I use XmlSerializer on it and street and town are empty I get this in the XML output,

 <Address />

Is there a way not to output this empty tag?

Thanks


Solution

  • You may implement IXmlSerializable and implement the serialization routine on your own. This way, you can avoid the element.

    An example here: http://paltman.com/2006/jul/03/ixmlserializable-a-persistable-example/