System.Xml.XmlConvert.ToDateTime C# (CSharp) Method

ToDateTime() public static method

public static ToDateTime ( string s, XmlDateTimeSerializationMode dateTimeOption ) : System.DateTime
s string
dateTimeOption XmlDateTimeSerializationMode
return System.DateTime
        public static DateTime ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption)
        {
            XsdDateTime xsdDateTime = new XsdDateTime(s, XsdDateTimeFlags.AllXsd);
            DateTime dt = (DateTime)xsdDateTime;

            switch (dateTimeOption)
            {
                case XmlDateTimeSerializationMode.Local:
                    dt = SwitchToLocalTime(dt);
                    break;

                case XmlDateTimeSerializationMode.Utc:
                    dt = SwitchToUtcTime(dt);
                    break;

                case XmlDateTimeSerializationMode.Unspecified:
                    dt = new DateTime(dt.Ticks, DateTimeKind.Unspecified);
                    break;

                case XmlDateTimeSerializationMode.RoundtripKind:
                    break;

                default:
                    throw new ArgumentException(SR.Format(SR.Sch_InvalidDateTimeOption, dateTimeOption, nameof(dateTimeOption)));
            }
            return dt;
        }

Same methods

XmlConvert::ToDateTime ( string s ) : System.DateTime
XmlConvert::ToDateTime ( string s, string format ) : System.DateTime

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Reads the error data in XML attributes.
        /// </summary>

        private static void ReadXmlAttributes(XmlReader reader, Error error)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (!reader.IsStartElement())
            {
                throw new ArgumentException("Reader is not positioned at the start of an element.", "reader");
            }

            error.ApplicationName = reader.GetAttribute("application");
            error.HostName        = reader.GetAttribute("host");
            error.Type            = reader.GetAttribute("type");
            error.Message         = reader.GetAttribute("message");
            error.Source          = reader.GetAttribute("source");
            error.Detail          = reader.GetAttribute("detail");
            error.User            = reader.GetAttribute("user");
            string timeString = Mask.NullString(reader.GetAttribute("time"));

            error.Time = timeString.Length == 0 ? new DateTime() : XmlConvert.ToDateTime(timeString);
            string statusCodeString = Mask.NullString(reader.GetAttribute("statusCode"));

            error.StatusCode         = statusCodeString.Length == 0 ? 0 : XmlConvert.ToInt32(statusCodeString);
            error.WebHostHtmlMessage = reader.GetAttribute("webHostHtmlMessage");
        }
All Usage Examples Of System.Xml.XmlConvert::ToDateTime