如何让XmlSerializer忽略某种类型的所有成员?

时间:2017-02-21 19:51:59

标签: c# .net xml-serialization xmlserializer

我想将XML反序列化为以下类:

public partial class Delivery
{
    public System.Nullable<System.DateTime> sentDate { get; set; }
    public System.Nullable<System.DateTime> receivedDate { get; set; }
    public System.Nullable<System.DateTime> responseDueDate { get; set; }
}

但是,XML中的日期不是XmlSerializer友好格式。根据多个问题的答案,我添加了这个课程:

public partial class DateSafeDelivery : Delivery
{
    [XmlElement("sentDate")]
    public string sentDateString
    {
        internal get { return sentDate.HasValue ? XmlConvert.ToString(sentDate.Value) : null; }
        set { sentDate = DateTime.Parse(value); }
    }
    [XmlElement("receivedDate")]
    public string receivedDateString
    {
        internal get { return receivedDate.HasValue ? XmlConvert.ToString(receivedDate.Value) : null; }
        set { receivedDate = DateTime.Parse(value); }
    }
    [XmlElement("responseDueDate")]
    public string responseDueDateString
    {
        internal get { return responseDueDate.HasValue ? XmlConvert.ToString(responseDueDate.Value) : null; }
        set { responseDueDate = DateTime.Parse(value); }
    }
}

然后我配置我的覆盖:

private static XmlAttributeOverrides GetOverrides()
{
    var overrides = new XmlAttributeOverrides();
    var attributes = new XmlAttributes();
    attributes.XmlElements.Add(new XmlElementAttribute(typeof(DateSafeDelivery)));
    overrides.Add(typeof(MyParent), "Delivery", attributes);
    var ignore = new XmlAttributes { XmlIgnore = true };
    overrides.Add(typeof(DateTime?), ignore);
    return overrides;
}

这导致以下期望:

Message=The string '2010-06-12T00:00:00 -05:00' is not a valid AllXsd value.
Source=System.Xml.ReaderWriter
StackTrace:
    at System.Xml.Schema.XsdDateTime..ctor(String text, XsdDateTimeFlags kinds)
    at System.Xml.XmlConvert.ToDateTime(String s, XmlDateTimeSerializationMode dateTimeOption)
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDeserializedAudit.Read1_NullableOfDateTime(Boolean checkType)
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDeserializedAudit.Read15_DateSafeDelivery(Boolean isNullable, Boolean checkType)
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDeserializedAudit.Read16_MyParent(Boolean isNullable, Boolean checkType)

因此正在使用DateSafeDelivery,但日期的XmlIgnore正在被忽略。

如果我切换它会工作:

    overrides.Add(typeof(DateTime?), ignore);

使用:

    new Dictionary<string, Type>()
    {
        { "sentDate", typeof(Delivery) },
        { "receivedDate", typeof(Delivery) },
        { "responseDueDate", typeof(Delivery) },
    }
        .ToList()
        .ForEach(t1 => overrides.Add(t1.Value, t1.Key, ignore));

对于一个班级和三个属性来说这很好。但我有14个课程,总共有三十个日期属性。我知道我必须为14个类添加覆盖,但有没有办法让序列化器忽略所有DateTime属性?

我认为XmlAttributeOverrides.Add Method (Type, XmlAttributes)会这样做。但它没有用。为什么?这个方法是什么用的?它做了什么?

1 个答案:

答案 0 :(得分:1)

XmlAttributeOverrides.Add(Type, XmlAttributes)旨在向类型本身添加XML覆盖属性,而不是向返回该类型值的所有属性添加。例如。如果您想向DateSafeDelivery添加[XmlRoot("OverrideName")]属性,可以执行以下操作:

overrides.Add(typeof(DateSafeDelivery),
    new XmlAttributes { XmlRoot = new XmlRootAttribute("OverrideName") });

没有动态覆盖属性来忽略返回给定类型的所有属性,因为没有static XML serialization attribute可以抑制给定类型的所有属性的序列化。以下甚至不编译,因为[XmlIgnore]只能应用于属性或字段:

[XmlIgnore] public class IgnoreAllInstancesOfMe { } // Fails to compile.

(至于为什么Microsoft没有实现对应用于某个类型的[XmlIgnore]的支持 - 你需要问他们。)

因此,您需要引入如下的扩展方法:

public static partial class XmlAttributeOverridesExtensions
{
    public static XmlAttributeOverrides IgnorePropertiesOfType(this XmlAttributeOverrides overrides, Type declaringType, Type propertyType)
    {
        return overrides.IgnorePropertiesOfType(declaringType, propertyType, new HashSet<Type>());
    }

    public static XmlAttributeOverrides IgnorePropertiesOfType(this XmlAttributeOverrides overrides, Type declaringType, Type propertyType, HashSet<Type> completedTypes)
    {
        if (overrides == null || declaringType == null || propertyType == null || completedTypes == null)
            throw new ArgumentNullException();
        XmlAttributes attributes = null;
        for (; declaringType != null && declaringType != typeof(object); declaringType = declaringType.BaseType)
        {
            // Avoid duplicate overrides.
            if (!completedTypes.Add(declaringType))
                break;
            foreach (var property in declaringType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
            {
                if (property.PropertyType == propertyType || Nullable.GetUnderlyingType(property.PropertyType) == propertyType)
                {
                    attributes = attributes ?? new XmlAttributes { XmlIgnore = true };
                    overrides.Add(declaringType, property.Name, attributes);
                }
            }
        }
        return overrides;
    }
}

并做:

    private static XmlAttributeOverrides GetOverrides()
    {
        var overrides = new XmlAttributeOverrides();

        var attributes = new XmlAttributes();
        attributes.XmlElements.Add(new XmlElementAttribute(typeof(DateSafeDelivery)));
        overrides.Add(typeof(MyParent), "Delivery", attributes);

        // Ignore all DateTime properties in DateSafeDelivery
        var completed = new HashSet<Type>();
        overrides.IgnorePropertiesOfType(typeof(DateSafeDelivery), typeof(DateTime), completed);
        // Add the other 14 types as required

        return overrides;
    }

另请注意,DateString上的DateSafeDelivery属性必须包含 public 获取和设置方法,例如:

public partial class DateSafeDelivery : Delivery
{
    [XmlElement("sentDate")]
    public string sentDateString
    {
        get { return sentDate.HasValue ? XmlConvert.ToString(sentDate.Value, XmlDateTimeSerializationMode.Utc) : null; }
        set { sentDate = DateTime.Parse(value); }
    }

XmlSerializer无法序列化不完全公开的属性。

顺便提一下,请注意您必须静态缓存由覆盖构建的任何XmlSerializer以避免严重的内存泄漏,如this answer中所述。