Partilhar via


XmlConvert.ToString Method (DateTime, XmlDateTimeSerializationMode)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the DateTime to a String using the XmlDateTimeSerializationMode specified.

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

Syntax

'Declaration
Public Shared Function ToString ( _
    value As DateTime, _
    dateTimeOption As XmlDateTimeSerializationMode _
) As String
public static string ToString(
    DateTime value,
    XmlDateTimeSerializationMode dateTimeOption
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentException

The dateTimeOption value is not valid.

ArgumentNullException

The value or dateTimeOption value is nulla null reference (Nothing in Visual Basic).

Examples

'Define the order data.  They will be converted to string 
'before being written out.
Dim custID As Int16 = 32632
Dim orderID As String = "367A54"
Dim orderDate As New DateTime()
orderDate = DateTime.Now
Dim price As Double = 19.95

Dim output As New StringBuilder()

'Create a writer that outputs to the console.
Dim writer As XmlWriter = XmlWriter.Create(output)

'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()


' Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString()

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

StringBuilder output = new StringBuilder();

//Create a writer that outputs to the console.
using (XmlWriter writer = XmlWriter.Create(output))
{
    //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
}

// Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.