I have the same problem with regard to this question. I am sending data back and forth with a SOAP-based API where the responses don't follow the standard quite right, specifically with null values. For DateTime, the API will send back an empty string, like this:
<nextreview></nextreview>
Which causes the following error to occur on deserialization:
The string '' is not a valid AllXsd value.
So my thought was to create a custom Nullable type, NullableOrEmpty<T>, implementing IXMLSerializable that handles empty string by converting it to null. The problem is I only want to handle the exceptional case of an empty string. Everything else I want to serialize and deserialize as normal, using the 'default' behavior. How do I simulate the default behavior of serialization in my code below?
public class NullableOrEmpty<T> : IXmlSerializable
where T : struct
{
public T? NullableValue { get; set; }
public T Value { get { return this.NullableValue.Value; } }
public bool HasValue { get { return this.NullableValue.HasValue; } }
...
public void ReadXml(XmlReader reader)
{
string xml = reader.ReadElementContentAsString();
if (string.IsNullOrEmpty(xml))
{
this.NullableValue = null;
}
else
{
//THIS SHOULD DO THE DEFAULT. THIS DOESN'T WORK. WHAT DO I DO??
//this.NullableValue = (T?)new XmlSerializer(typeof(T?)).Deserialize(reader);
}
}
public void WriteXml(XmlWriter writer)
{
//THIS SHOULD DO THE DEFAULT. THIS DOESN'T WORK. WHAT DO I DO??
//new XmlSerializer(typeof(T?)).Serialize(writer, this.NullableValue);
}
}
When I say "THIS DOESN'T WORK", it specifically generates the following error message, probably because it's trying to consume something that isn't there:
There is an error in XML document (63, 6).
<lastreview xmlns=''>was not expected.
Here is a snippet of XML at that spot. The error is caused by the value in birthdate, because I'm not consuming it correctly in the non-exceptional case where the value is actually given:
<udf4></udf4>
<udf3></udf3>
<birthdate>1978-05-24Z</birthdate>
<lastreview></lastreview>
<fulltime>1</fulltime>
Any thoughts or ideas are appreciated. I can post more code samples if needed or test out suggestions. Thanks!
XmlReader. You've already read past the content you want to read again.