If you want to take control of xml serialization, you have two options. The first (which may not be acceptable here) is to use attributes in the System.Xml.Serialization namespace to exclude properties. If you really need to determine what is being serialized at runtime, this might not be the best course of action.
See Attributes That Control XML Serialization
Another way to do this is to implement the IXmlSerializable interface in your class and implement the ReadXml and WriteXml methods. This allows you to precisely control how your xml looks. See this question for more information:
custom XML serialization
However, as mentioned here Mixing custom and basic serialization? after implementing IXmlSerializable, you are responsible for all the serialization logic for your type.
Martin peck
source share