Note that there are some explanatory texts on larger screens.

plurals
  1. POHandle Empty String as Null in XML Deserialization
    primarykey
    data
    text
    <p>I have the same problem with regard to <a href="https://stackoverflow.com/questions/644964/when-implementing-ixmlserializable-how-to-only-override-either-readxml-or-writex">this question</a>. 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 <code>DateTime</code>, the API will send back an empty string, like this:</p> <pre><code>&lt;nextreview&gt;&lt;/nextreview&gt; </code></pre> <p>Which causes the following error to occur on deserialization:</p> <blockquote> <p>The string '' is not a valid AllXsd value.</p> </blockquote> <p>So my thought was to create a custom Nullable type, <code>NullableOrEmpty&lt;T&gt;</code>, implementing <code>IXMLSerializable</code> 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? </p> <pre><code>public class NullableOrEmpty&lt;T&gt; : 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); } } </code></pre> <p>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:</p> <blockquote> <p>There is an error in XML document (63, 6).</p> <p><code>&lt;lastreview xmlns=''&gt;</code> was not expected.</p> </blockquote> <p>Here is a snippet of XML at that spot. The error is caused by the value in <code>birthdate</code>, because I'm not consuming it correctly in the non-exceptional case where the value is actually given:</p> <pre><code>&lt;udf4&gt;&lt;/udf4&gt; &lt;udf3&gt;&lt;/udf3&gt; &lt;birthdate&gt;1978-05-24Z&lt;/birthdate&gt; &lt;lastreview&gt;&lt;/lastreview&gt; &lt;fulltime&gt;1&lt;/fulltime&gt; </code></pre> <p>Any thoughts or ideas are appreciated. I can post more code samples if needed or test out suggestions. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload