Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting element/attribute values with XML Serialization
    text
    copied!<p>I have a class that represents credit card details. To represent valid from and expiration months and years I am using four properties of type <code>int</code>:</p> <pre><code>public int ValidFromMonth { get; set; } public int ValidFromYear { get; set; } public int ExpiresEndMonth { get; set; } public int ExpiresEndYear { get; set; } </code></pre> <p>I am XML Serializing this class for consumption by a third party. That third party requires my month and year values to be prefixed with a leading zero if the value is less than 10</p> <pre><code>&lt;validFromMonth&gt;02&lt;/validFromMonth&gt; &lt;validFromYear&gt;09&lt;/validFromYear&gt; &lt;expiresEndMonth&gt;10&lt;/expiresEndMonth&gt; &lt;expiresEndYear&gt;14&lt;/expiresEndYear&gt; </code></pre> <p>Does .NET support any attribution (or is it possible for me to create a custom attribute) that will enforce this rule, possibly using a format string (e.g. <code>{0:00}</code>)?</p> <p>Note: I know that I could add my own <code>string</code> properties that do the formatting internally, and add an <code>[XmlIgnore]</code> attribute to my <code>int</code> properties, but this feels like a second-rate solution.</p> <p><strong>Edit:</strong> After some consideration I am wondering if this is actually just not feasible. Serialization would be no problem, but in order for deserialization to work you would need to un-format the serialized string. In the trivial example above this would be easy, but I am not sure that it could be made to work in the more general case.</p> <p><strong>Edit2:</strong> The XML Schema that defines the two-digit requirement is below.</p> <p>Simple type definitions:</p> <pre><code>&lt;xs:simpleType name="CreditCardMonthType"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Two digit month&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="2" /&gt; &lt;xs:maxLength value="2" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;xs:simpleType name="CreditCardYearType"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Two digit year&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="2" /&gt; &lt;xs:maxLength value="2" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; </code></pre> <p>Credit card definition that uses these types:</p> <pre><code>&lt;xs:attribute name="ExpiryMonth" type="CreditCardMonthType" use="required"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Credit/debt card's expiry month.&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;/xs:attribute&gt; &lt;xs:attribute name="ExpiryYear" type="CreditCardYearType" use="required"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Credit/debt card's expiry year.&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;/xs:attribute&gt; &lt;xs:attribute name="StartMonth" type="CreditCardMonthType" use="optional"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Switch card's start month.&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;/xs:attribute&gt; &lt;xs:attribute name="StartYear" type="CreditCardYearType" use="optional"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;Switch card's start year.&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;/xs:attribute&gt; </code></pre>
 

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