I want to deserialize XML into the following class:
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; }
}
However, the dates in the XML are not in an XmlSerializer friendly format. Based on answers to mulitple questions, I added this class:
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); }
}
}
I then configure my 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);
var ignore = new XmlAttributes { XmlIgnore = true };
overrides.Add(typeof(DateTime?), ignore);
return overrides;
}
This results in the following expection:
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)
So DateSafeDelivery
is being used, but the XmlIgnore
for the dates is being, well, ignored.
It will work, if I switch:
overrides.Add(typeof(DateTime?), ignore);
with:
new Dictionary<string, Type>()
{
{ "sentDate", typeof(Delivery) },
{ "receivedDate", typeof(Delivery) },
{ "responseDueDate", typeof(Delivery) },
}
.ToList()
.ForEach(t1 => overrides.Add(t1.Value, t1.Key, ignore));
And that's fine for one class and three properties. But I have 14 classes with a total of three dozen date properties. I know I have to add overrides for the 14 classes, but is there a way to get the serializer to ignore all DateTime
properties?
I thought XmlAttributeOverrides.Add Method (Type, XmlAttributes) would do it. But it isn't working. Why? What is this method for? What does it do?