Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlConvert::ToString Method (DateTime, String^)

 

Converts the DateTime to a String.

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

public:
static String^ ToString(
	DateTime value,
	String^ format
)

Parameters

value
Type: System::DateTime

The value to convert.

format
Type: System::String^

The format structure that defines how to display the converted string. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

Return Value

Type: System::String^

A string representation of the DateTime in the specified format.

The following example, converts data types to string and then writes the information out to the console.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{

   //Define the order data.  They will be converted to string 
   //before being written out.
   Int16 custID = 32632;
   String^ orderID = "367A54";
   DateTime orderDate = DateTime::Now;
   Double price = 19.95;

   //Create a writer that outputs to the console.
   XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );
   writer->Formatting = Formatting::Indented;

   //Write an element (this one is the root)
   writer->WriteStartElement( "order" );

   //Write the order date.
   writer->WriteAttributeString( "date", XmlConvert::ToString( orderDate, "yyyy-MM-dd" ) );

   //Write the order time.
   writer->WriteAttributeString( "time", XmlConvert::ToString( orderDate, "HH:mm:ss" ) );

   //Write the order data.
   writer->WriteElementString( "orderID", orderID );
   writer->WriteElementString( "custID", XmlConvert::ToString( custID ) );
   writer->WriteElementString( "price", XmlConvert::ToString( price ) );

   //Write the close tag for the root element
   writer->WriteEndElement();

   //Write the XML and close the writer
   writer->Close();
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2017 Microsoft