Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:45:03+00:00 2026-05-11T15:45:03+00:00

I have a class that represents credit card details. To represent valid from and

  • 0

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 int:

public int ValidFromMonth { get; set; } public int ValidFromYear { get; set; } public int ExpiresEndMonth { get; set; } public int ExpiresEndYear { get; set; } 

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

<validFromMonth>02</validFromMonth> <validFromYear>09</validFromYear> <expiresEndMonth>10</expiresEndMonth> <expiresEndYear>14</expiresEndYear> 

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. {0:00})?

Note: I know that I could add my own string properties that do the formatting internally, and add an [XmlIgnore] attribute to my int properties, but this feels like a second-rate solution.

Edit: 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.

Edit2: The XML Schema that defines the two-digit requirement is below.

Simple type definitions:

<xs:simpleType name='CreditCardMonthType'>   <xs:annotation>    <xs:documentation>Two digit month</xs:documentation>   </xs:annotation>   <xs:restriction base='xs:string'>    <xs:minLength value='2' />    <xs:maxLength value='2' />   </xs:restriction>  </xs:simpleType> <xs:simpleType name='CreditCardYearType'>   <xs:annotation>    <xs:documentation>Two digit year</xs:documentation>   </xs:annotation>   <xs:restriction base='xs:string'>    <xs:minLength value='2' />    <xs:maxLength value='2' />   </xs:restriction> </xs:simpleType> 

Credit card definition that uses these types:

<xs:attribute name='ExpiryMonth' type='CreditCardMonthType' use='required'>  <xs:annotation>   <xs:documentation>Credit/debt card's expiry month.</xs:documentation>  </xs:annotation> </xs:attribute> <xs:attribute name='ExpiryYear' type='CreditCardYearType' use='required'>  <xs:annotation>   <xs:documentation>Credit/debt card's expiry year.</xs:documentation>  </xs:annotation> </xs:attribute> <xs:attribute name='StartMonth' type='CreditCardMonthType' use='optional'>  <xs:annotation>   <xs:documentation>Switch card's start month.</xs:documentation>  </xs:annotation> </xs:attribute> <xs:attribute name='StartYear' type='CreditCardYearType' use='optional'>  <xs:annotation>   <xs:documentation>Switch card's start year.</xs:documentation>  </xs:annotation> </xs:attribute> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:45:04+00:00Added an answer on May 11, 2026 at 3:45 pm

    OK, ignore my previous code sample (I’ll leave it up since it might help somebody else, though). I just remembered you can do this using XmlEnumAttribute:

    public enum LeadingZeroMonth {     [XmlEnum('01')]     January,      ...      [XmlEnum('12')]     December } 

    and then change your usage to the enum:

    public LeadingZeroMonth ValidFromMonth { get; set; } 

    This is actually a very nice way since you now have an enum for the month (which is really what you should’ve done from the beginning).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class that represents a product to be priced using one
I'm using JAXB for quick-and-dirty XML generation. I have a class that represents a
I have a base class that represents a database test in TestNG, and I
I have a data structure that represents C# code like this: class Namespace: string
I have a string variable that represents the name of a custom class. Example:
Let's say I have a table that represents a super class, students . And
I have a class that I wish to expose as a remote service using
I have class that represents users. Users are divided into two groups with different
I have a class that represents a row in a table. I want to
I have a class that represents the state of the world. The class has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.