Excluding some properties during serialization without changing the original class
Solution 1:
For whoever is interested, I decided to use XmlAttributeOverrides
, but made them more strong typed (I hate to type property names as strings). Here is the extension method I used for it:
public static void Add<T>(this XmlAttributeOverrides overrides, Expression<Func<T, dynamic>> propertySelector, XmlAttributes attributes)
{
overrides.Add(typeof(T), propertySelector.BuildString(), attributes);
}
public static string BuildString(this Expression propertySelector)
{
switch (propertySelector.NodeType)
{
case ExpressionType.Lambda:
LambdaExpression lambdaExpression = (LambdaExpression)propertySelector;
return BuildString(lambdaExpression.Body);
case ExpressionType.Convert:
case ExpressionType.Quote:
UnaryExpression unaryExpression = (UnaryExpression)propertySelector;
return BuildString(unaryExpression.Operand);
case ExpressionType.MemberAccess:
MemberExpression memberExpression = (MemberExpression)propertySelector;
MemberInfo propertyInfo = memberExpression.Member;
if (memberExpression.Expression is ParameterExpression)
{
return propertyInfo.Name;
}
else
{
// we've got a nested property (e.g. MyType.SomeProperty.SomeNestedProperty)
return BuildString(memberExpression.Expression) + "." + propertyInfo.Name;
}
default:
// drop out and throw
break;
}
throw new InvalidOperationException("Expression must be a member expression: " + propertySelector.ToString());
}
Then, to ignore an attribute, I can beautifully add it to the ignore list:
var overrides = new XmlAttributeOverrides();
var ignore = new XmlAttributes { XmlIgnore = true };
overrides.Add<MyClass>(m => m.Id, ignore);
overrides.Add<MyClass>(m => m.DateChanged, ignore);
Type t = typeof(List<MyClass>);
XmlSerializer serial = new XmlSerializer(t, overrides);
Solution 2:
You may be able to exclude some properties by taking advantage of the fact that the XmlSerializer
will not serialize nulls to the output. So for reference types, you can null out those properties you don't want to appear in the xml.
The resulting xml will be deserializable back into the same class, but the omitted fields will obviously be null.
However, this doesn't help for your desire to change the date format. For this, you'll need to either create a new class that has the date as a string in the format you want, or you could implement IXmlSerializable
, giving you complete control over the xml. [it is worth noting that date data-type has a standard format in XML, so by changing it it won't stricly be an XML date any longer - you may not care].
[EDIT in response to your comments]
There is an additional trick you might use to "disappear" a null nullable type, but it does require a change to your class.
The serializer, when serializing MyProperty
- will also check if there is a property called MyProperySpecified
. If it exists and returns false, the item property is not serialized:
public class Person
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public DateTime? BirthDate { get; set; }
public bool BirthDateSpecified
{
get { return BirthDate.HasValue; }
}
}
If you are prepared to add this property you can make it remove the nullable types when null. In fact - now I think about it - this may be a useful way of removing other properties too, depending on your usage scenario.