XmlConvert.ToString 方法

定義

將強型別資料轉換為等效 String 的表示。

多載

名稱 Description
ToString(Single)

將 轉換為 SingleString

ToString(TimeSpan)

將 轉換為 TimeSpanString

ToString(UInt16)

將 轉換為 UInt16String

ToString(UInt32)

將 轉換為 UInt32String

ToString(DateTimeOffset, String)

將所提供的 DateTimeOffset 轉換為指定格式的 a String

ToString(DateTime, String)

將 轉換為 DateTimeString

ToString(DateTime, XmlDateTimeSerializationMode)

利用指定方式DateTime將 轉換為 String aXmlDateTimeSerializationMode

ToString(SByte)

將 轉換為 SByteString

ToString(UInt64)

將 轉換為 UInt64String

ToString(Int64)

將 轉換為 Int64String

ToString(Boolean)

將 轉換為 BooleanString

ToString(Int16)

將 轉換為 Int16String

ToString(Guid)

將 轉換為 GuidString

ToString(Double)

將 轉換為 DoubleString

ToString(Decimal)

將 轉換為 DecimalString

ToString(DateTimeOffset)

將 sserved DateTimeOffsetString轉換為 。

ToString(DateTime)
已淘汰.
已淘汰.

將 轉換為 DateTimeString

ToString(Char)

將 轉換為 CharString

ToString(Byte)

將 轉換為 ByteString

ToString(Int32)

將 轉換為 Int32String

ToString(Single)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 SingleString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 TimeSpanString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

重要

此 API 不符合 CLS 規範。

將 轉換為 UInt16String

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

重要

此 API 不符合 CLS 規範。

將 轉換為 UInt32String

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將所提供的 DateTimeOffset 轉換為指定格式的 a 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

格式會被轉換成 value 。 格式參數可以是 W3C 建議中 XML dateTime 類型的任何子集。 (更多資訊請參閱 XML Schema 規範中的 dateTime 章節。)

傳回

String 指定格式表示所提供的 DateTimeOffset

範例

以下範例將 DateTimeOffset 當前時間的表示轉換為指定格式的 a 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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 DateTimeString

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;
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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

利用指定方式DateTime將 轉換為 String aXmlDateTimeSerializationMode

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

參數

value
DateTime

DateTime轉換價值。

dateTimeOption
XmlDateTimeSerializationMode

XmlDateTimeSerializationMode其中一個數值會指定如何處理該DateTime數值。

傳回

相當 String 於 的 DateTime

例外狀況

這個 dateTimeOption 數值並不成立。

valuedateTimeOptionnull

適用於

ToString(SByte)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

重要

此 API 不符合 CLS 規範。

將 轉換為 SByteString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

重要

此 API 不符合 CLS 規範。

將 轉換為 UInt64String

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 Int64String

public:
 static System::String ^ ToString(long value);
public static string ToString(long value);
static member ToString : int64 -> string
Public Shared Function ToString (value As Long) As String

參數

value
Int64

要轉換的值。

傳回

一個字串表示。Int64

適用於

ToString(Boolean)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 BooleanString

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串表示,即「真」或「假」。

適用於

ToString(Int16)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 Int16String

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;
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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 GuidString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 DoubleString

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;
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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 DecimalString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 sserved DateTimeOffsetString轉換為 。

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他們將被轉化。

傳回

String所提供的 DateTimeOffset表示。

範例

以下範例將 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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

警告

Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.

警告

Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode

將 轉換為 DateTimeString

public:
 static System::String ^ ToString(DateTime value);
[System.Obsolete("Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.")]
public static string ToString(DateTime value);
[System.Obsolete("Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode")]
public static string ToString(DateTime value);
public static string ToString(DateTime value);
[<System.Obsolete("Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.")>]
static member ToString : DateTime -> string
[<System.Obsolete("Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode")>]
static member ToString : DateTime -> string
static member ToString : DateTime -> string
Public Shared Function ToString (value As DateTime) As String

參數

value
DateTime

要轉換的值。

傳回

以 yyyy-MM-ddTHH:mm:ss 格式表示的 DateTime 字串表示,其中「T」為常數文字。

屬性

備註

Note

XmlConvert.ToString(DateTime) 方法在 2.0 版本的 .NET 框架中已過時,並被 XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode) 方法取代。 建議模式為 RoundtripKind。 若預期完全吻合,請使用 XmlConvert.ToString(DateTime, String) 格式字串 yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz

適用於

ToString(Char)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 CharString

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)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 ByteString

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

適用於

ToString(Int32)

來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs
來源:
XmlConvert.cs

將 轉換為 Int32String

public:
 static System::String ^ ToString(int value);
public static string ToString(int value);
static member ToString : int -> string
Public Shared Function ToString (value As Integer) As String

參數

value
Int32

要轉換的值。

傳回

一個字串表示。Int32

適用於