--------XML SerializableAttribute 前陣子碰到一個需求:因為DateTime型別的物件在進行XmlSerialize(XML序列化)的時候,會轉換成ISO 8601的字串格式,變成:2010-03-02T02:49:35.235,結果對方的程式沒辦法直接轉換,希望我可以改成一般常用的:2010/03/02 02:49。 通常如果要自訂序列化的方式,都會實作IXmlSerializable介面,但今天只有一個日期欄位想要轉換輸出格式的時候,有沒有更好的方式呢? 利用XML SerializableAttribute,我們可以幫他做個小小變化 //原先屬性 02 [XmlIgnore] 03 public DateTime CreateTime { get; set; } 04 05 //替代屬性 06 [XmlAttribute("CreateTime")] 07 public string XCreateTime 08 { 09 get { return this.CreateTime.ToString("yyyy/MM/dd hh:mm"); } 10 set { this.CreateTime = new DateTime.Parse(value); } 11 } --------ISO8601 What ISO 8601 covers The standard offers representations for the following: ■Date ■Time of the day ■Coordinated universal time (UTC) ■Local time with offset to UTC ■Date and time ■Time intervals ■Recurring time intervals Representations can be in one of two formats: a basic format that has a minimal number of characters and an extended format that adds characters to enhance human readability. For example, the third of January 2003 can be represented as either 20030103 or 2003-01-03. How it works – examples ISO 8601 advises numeric representation of dates and times on an internationally agreed basis. It represents elements from the largest to the smallest element: year-month-day: ■Calendar date is the most common date representation. It is: YYYY-MM-DD where YYYY is the year in the Gregorian calendar, MM is the month of the year between 01 (January) and 12 (December), and DD is the day of the month between 01 and 31. Example: 2003-04-01 represents the first day of April in 2003. ■Week date is an alternative date representation used in many commercial and industrial applications. It is: YYYY-Www-D where YYYY is the Year in the Gregorian calendar, ww is the week of the year between 01 (the first week) and 52 or 53 (the last week), and D is the day in the week between 1 (Monday) and 7 (Sunday). Example: 2003-W14-2 represents the second day of the fourteenth week of 2003. ■Time of the day is the time representation, using the 24-hour timekeeping system. It is: hh:mm:ss where hh is the number of complete hours that have passed since midnight, mm is the number of complete minutes since the start of the hour, and ss is the number of complete seconds since the start of the minute. Example: 23:59:59 represents the time one second before midnight. ■Date and time represents a specified time of a specified day. When use is made of the calendar date the representation is: YYYY-MM-DDThh:mm:ss where the capital letter T is used to separate the date and time components. Thus, for a very precise date and time, look at this: Example: 2003-04-01T13:01:02 represents one minute and two seconds after one o'clock in the afternoon of 2003-04-01. The standard has provisions for: ■the omission of components representing smaller units (seconds, minutes), where such precision is not needed, ■the addition of a decimal fraction to the smallest time unit where higher precision is needed. Advantages of ISO 8601 The representations of ISO 8601 offer the following advantages over many of the locally used representations: ■Easily readable and writeable by systems ■Easily comparable and sortable ■Language independent ■Larger units are written in front of smaller units ■For most representations the notation is short and of constant length Some of the representations permitted by the standard can be used without further agreement. Others should only be used by mutual agreement between the communicating partners; such agreement could be established for instance by mentioning the representation explicitly in a standard or specification applying the representation. This will facilitate processing at the receiving and may be necessary to avoid ambiguity when interpreting ISO 8601 expressions. Specifications applying ISO 8601 can have a need to define the allowed representation of the expressions in a certain data field. For instance a specification might want to define that a certain field containing the date and time of an event should do so in the format: YYYYMMDDhhmm with the exclusion of all other formats allowed by ISO 8601. In such cases the meta language used internally in ISO 8601 can be used. ISO 8601 does not specify the exact meaning of its representations. This means for example that the standard does not define whether 09:00 refers to the exact end of the ninth hour of the day, any other point in the following minute or the minute as a whole. Users of the standard must agree on a more exact interpretation of the representation if this is relevant.