XmlConvert.ToString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將強型別資料轉換為對等的 String 表示。
多載
ToString(Single)
public:
static System::String ^ ToString(float value);
public static string ToString (float value);
static member ToString : single -> string
Public Shared Function ToString (value As Single) As String
參數
- value
- Single
要進行轉換的值。
傳回
Single 的字串表示。
備註
如果 value
是 Single.PositiveInfinity 或 Single.NegativeInfinity,這個方法會分別傳回字串 INF 或 -INF。
另請參閱
適用於
ToString(TimeSpan)
public:
static System::String ^ ToString(TimeSpan value);
public static string ToString (TimeSpan value);
static member ToString : TimeSpan -> string
Public Shared Function ToString (value As TimeSpan) As String
參數
- value
- TimeSpan
要進行轉換的值。
傳回
TimeSpan
的字串表示。
適用於
ToString(UInt16)
重要
此 API 不符合 CLS 規範。
public:
static System::String ^ ToString(System::UInt16 value);
[System.CLSCompliant(false)]
public static string ToString (ushort value);
[<System.CLSCompliant(false)>]
static member ToString : uint16 -> string
Public Shared Function ToString (value As UShort) As String
參數
- value
- UInt16
要進行轉換的值。
傳回
UInt16 的字串表示。
- 屬性
適用於
ToString(UInt32)
重要
此 API 不符合 CLS 規範。
public:
static System::String ^ ToString(System::UInt32 value);
[System.CLSCompliant(false)]
public static string ToString (uint value);
[<System.CLSCompliant(false)>]
static member ToString : uint32 -> string
Public Shared Function ToString (value As UInteger) As String
參數
- value
- UInt32
要進行轉換的值。
傳回
UInt32 的字串表示。
- 屬性
適用於
ToString(DateTimeOffset, String)
將提供的 DateTimeOffset 轉換成指定格式的 String。
public:
static System::String ^ ToString(DateTimeOffset value, System::String ^ format);
public static string ToString (DateTimeOffset value, string format);
static member ToString : DateTimeOffset * string -> string
Public Shared Function ToString (value As DateTimeOffset, format As String) As String
參數
- value
- DateTimeOffset
要轉換的 DateTimeOffset。
- format
- String
s
所要轉換成的格式。 格式參數可以是 XML dateTime 型別之 W3C Recommendation 的任何子集 (如需詳細資訊,請參閱 XML 結構描述規格的 dateTime 一節。)
傳回
以所提供之 DateTimeOffset 指定格式的 String 表示。
範例
下列範例會 DateTimeOffset 以指定的格式,將目前時間 String 的表示轉換為 。
using System;
using System.Xml;
class Example
{
static void Main()
{
// Create the DateTimeOffset object and set the time to the current time.
DateTimeOffset dto;
dto = DateTimeOffset.Now;
// Convert the DateTimeObject to a string in a specified format and display the result.
// The specified format must be a subset of the W3C Recommendation for the XML dateTime type.
String timeAsString = XmlConvert.ToString(dto, "yyyy-MM-ddTHH:mm:sszzzzzzz");
Console.WriteLine(timeAsString);
}
}
Imports System.Xml
Module Module1
Sub Main()
' Create the DateTimeOffset object and set the time to the current time.
Dim dto As DateTimeOffset
dto = DateTimeOffset.Now
' Convert the DateTimeObject to a string in a specified format and display the result.
' The specified format must be a subset of the W3C Recommendation for the XML dateTime type.
Dim timeAsString As [String] = XmlConvert.ToString(dto, "yyyy-MM-ddTHH:mm:sszzzzzzz")
Console.WriteLine(timeAsString)
End Sub
End Module
適用於
ToString(DateTime, String)
public:
static System::String ^ ToString(DateTime value, System::String ^ format);
public static string ToString (DateTime value, string format);
static member ToString : DateTime * string -> string
Public Shared Function ToString (value As DateTime, format As String) As String
參數
- value
- DateTime
要進行轉換的值。
- format
- String
定義如何顯示轉換後字串的格式結構。 有效格式包含 "yyyy-MM-ddTHH:mm:sszzzzzz" 及其子集。
傳回
DateTime
的指定格式字串表示。
範例
下列範例會將數據類型轉換成字串,然後將資訊寫出主控台。
#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();
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//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;
//Create a writer that outputs to the console.
XmlTextWriter writer = new 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();
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'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 DateTime
orderDate = DateTime.Now
Dim price as Double = 19.95
'Create a writer that outputs to the console.
Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
'Use indenting for readability
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.Flush()
writer.Close()
end sub
end class
適用於
ToString(DateTime, XmlDateTimeSerializationMode)
使用指定的 XmlDateTimeSerializationMode,將 DateTime 轉換為 String。
public:
static System::String ^ ToString(DateTime value, System::Xml::XmlDateTimeSerializationMode dateTimeOption);
public static string ToString (DateTime value, System.Xml.XmlDateTimeSerializationMode dateTimeOption);
static member ToString : DateTime * System.Xml.XmlDateTimeSerializationMode -> string
Public Shared Function ToString (value As DateTime, dateTimeOption As XmlDateTimeSerializationMode) As String
參數
- dateTimeOption
- XmlDateTimeSerializationMode
其中一個 XmlDateTimeSerializationMode 值,可指定如何處理 DateTime 值。
傳回
例外狀況
dateTimeOption
值不是有效的。
value
或 dateTimeOption
的值為 null
。
適用於
ToString(SByte)
重要
此 API 不符合 CLS 規範。
public:
static System::String ^ ToString(System::SByte value);
[System.CLSCompliant(false)]
public static string ToString (sbyte value);
[<System.CLSCompliant(false)>]
static member ToString : sbyte -> string
Public Shared Function ToString (value As SByte) As String
參數
- value
- SByte
要進行轉換的值。
傳回
SByte
的字串表示。
- 屬性
適用於
ToString(UInt64)
重要
此 API 不符合 CLS 規範。
public:
static System::String ^ ToString(System::UInt64 value);
[System.CLSCompliant(false)]
public static string ToString (ulong value);
[<System.CLSCompliant(false)>]
static member ToString : uint64 -> string
Public Shared Function ToString (value As ULong) As String
參數
- value
- UInt64
要進行轉換的值。
傳回
UInt64 的字串表示。
- 屬性
適用於
ToString(Int64)
ToString(Boolean)
public:
static System::String ^ ToString(bool value);
public static string ToString (bool value);
static member ToString : bool -> string
Public Shared Function ToString (value As Boolean) As String
參數
- value
- Boolean
要進行轉換的值。
傳回
Boolean
的字串表示,也就是 "true" 或 "false"。
適用於
ToString(Int16)
public:
static System::String ^ ToString(short value);
public static string ToString (short value);
static member ToString : int16 -> string
Public Shared Function ToString (value As Short) As String
參數
- value
- Int16
要進行轉換的值。
傳回
Int16 的字串表示。
範例
下列範例會將數據類型轉換成字串,然後將資訊寫出主控台。
#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();
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//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;
//Create a writer that outputs to the console.
XmlTextWriter writer = new 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();
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'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 DateTime
orderDate = DateTime.Now
Dim price as Double = 19.95
'Create a writer that outputs to the console.
Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
'Use indenting for readability
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.Flush()
writer.Close()
end sub
end class
適用於
ToString(Guid)
public:
static System::String ^ ToString(Guid value);
public static string ToString (Guid value);
static member ToString : Guid -> string
Public Shared Function ToString (value As Guid) As String
參數
- value
- Guid
要進行轉換的值。
傳回
Guid
的字串表示。
適用於
ToString(Double)
public:
static System::String ^ ToString(double value);
public static string ToString (double value);
static member ToString : double -> string
Public Shared Function ToString (value As Double) As String
參數
- value
- Double
要進行轉換的值。
傳回
Double 的字串表示。
範例
下列範例會將數據類型轉換成字串,然後將資訊寫出主控台。
#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();
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//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;
//Create a writer that outputs to the console.
XmlTextWriter writer = new 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();
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'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 DateTime
orderDate = DateTime.Now
Dim price as Double = 19.95
'Create a writer that outputs to the console.
Dim writer as XmlTextWriter = new XmlTextWriter (Console.Out)
'Use indenting for readability
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.Flush()
writer.Close()
end sub
end class
備註
如果 value
是 Double.PositiveInfinity 或 Double.NegativeInfinity,這個方法會分別傳回字串 INF 或 -INF。
另請參閱
適用於
ToString(Decimal)
public:
static System::String ^ ToString(System::Decimal value);
public static string ToString (decimal value);
static member ToString : decimal -> string
Public Shared Function ToString (value As Decimal) As String
參數
- value
- Decimal
要進行轉換的值。
傳回
Decimal
的字串表示。
適用於
ToString(DateTimeOffset)
將提供的 DateTimeOffset 轉換成 String。
public:
static System::String ^ ToString(DateTimeOffset value);
public static string ToString (DateTimeOffset value);
static member ToString : DateTimeOffset -> string
Public Shared Function ToString (value As DateTimeOffset) As String
參數
- value
- DateTimeOffset
要轉換的 DateTimeOffset。
傳回
所提供之 DateTimeOffset 的 String 表示。
範例
下列範例會將 DateTimeOffset 目前時間的表示轉換為 String。
using System;
using System.Xml;
class Example
{
static void Main()
{
// Create the DateTimeOffset object and set the time to the current time
DateTimeOffset dto;
dto = DateTimeOffset.Now;
// Convert the DateTimeOffset object to a string and display the result
string timeAsString = XmlConvert.ToString(dto);
Console.WriteLine(timeAsString);
}
}
Imports System.Xml
Module Module1
Sub Main()
' Create the DateTimeOffset object and set the time to the current time
Dim dto As DateTimeOffset
dto = DateTimeOffset.Now
' Convert the DateTimeOffset object to a string and display the result
Dim timeAsString As String = XmlConvert.ToString(dto)
Console.WriteLine(timeAsString)
End Sub
End Module
適用於
ToString(DateTime)
警告
Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode
警告
Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.
public:
static System::String ^ ToString(DateTime value);
[System.Obsolete("Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode")]
public static string ToString (DateTime value);
[System.Obsolete("Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.")]
public static string ToString (DateTime value);
public static string ToString (DateTime value);
[<System.Obsolete("Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode")>]
static member ToString : DateTime -> string
[<System.Obsolete("Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.")>]
static member ToString : DateTime -> string
static member ToString : DateTime -> string
Public Shared Function ToString (value As DateTime) As String
參數
- value
- DateTime
要進行轉換的值。
傳回
DateTime
的字串表示,格式為 yyyy-MM-ddTHH:mm:ss,其中 'T' 是常值。
- 屬性
備註
注意
方法XmlConvert.ToString(DateTime)在 2.0 版的 .NET Framework 中已過時,且已取代為 XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode) 方法。
建議的模式為 RoundtripKind。 如果預期完全相符,請使用 XmlConvert.ToString(DateTime, String) 格式字串 yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz
。
適用於
ToString(Char)
public:
static System::String ^ ToString(char value);
public static string ToString (char value);
static member ToString : char -> string
Public Shared Function ToString (value As Char) As String
參數
- value
- Char
要進行轉換的值。
傳回
Char
的字串表示。
適用於
ToString(Byte)
public:
static System::String ^ ToString(System::Byte value);
public static string ToString (byte value);
static member ToString : byte -> string
Public Shared Function ToString (value As Byte) As String
參數
- value
- Byte
要進行轉換的值。
傳回
Byte
的字串表示。