Export (0) Print
Expand All

XmlConvert.ToDateTime Method (String)

Note: This API is now obsolete.

Converts the String to a DateTime equivalent.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

[ObsoleteAttribute("Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode")]
public static DateTime ToDateTime(
	string s


Type: System.String

The string to convert.

Return Value

Type: System.DateTime
A DateTime equivalent of the string.


s is null.


s is an empty string or is not in the correct format.


The XmlConvert.ToDateTime(String) method is obsolete in the 2.0 version of the .NET Framework and has been replaced by the XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode) method.

The following example uses ToDouble and ToDateTime to read strongly typed data.

using System;
using System.IO;
using System.Xml;

public class Sample

  public static void Main()
    XmlTextReader reader = new XmlTextReader("orderData.xml");

    //Parse the file and pull out the order date and price. 
    while (reader.Read()){
       if (reader.NodeType==XmlNodeType.Element){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());

    //Close the reader.

The example uses the file, orderData.xml, as input.

<order date="2001-05-03">

.NET Framework

Supported in: 1.1, 1.0
Obsolete (compiler warning) in 4.5.2
Obsolete (compiler warning) in 4.5.1
Obsolete (compiler warning) in 4.5
Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2

.NET Framework Client Profile

Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft