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)

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

Parameters

s
Type: System::String^

The string to convert.

Return Value

Type: System::DateTime

A DateTime equivalent of the string.

Exception Condition
ArgumentNullException

s is null.

FormatException

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

System_CAPS_noteNote

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.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew XmlTextReader( "orderData.xml" );

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


   //Close the reader.
   reader->Close();
}

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

<order date="2001-05-03">
  <orderID>367A54</orderID>
  <custID>32632</custID>
  <price>19.95</price>
</order>

.NET Framework
Available since 1.1
Return to top
Show: