XmlConvert.ToString 方法
定义
重载
| ToString(Single) | |
| ToString(TimeSpan) | |
| ToString(UInt16) | |
| ToString(UInt32) | |
| ToString(DateTimeOffset, String) |
将提供的 DateTimeOffset 转换为指定格式的 String。Converts the supplied DateTimeOffset to a String in the specified format. |
| ToString(DateTime, String) | |
| ToString(DateTime, XmlDateTimeSerializationMode) |
使用指定的 XmlDateTimeSerializationMode 将 DateTime 转换为 String。Converts the DateTime to a String using the XmlDateTimeSerializationMode specified. |
| ToString(SByte) | |
| ToString(UInt64) | |
| ToString(Int64) | |
| ToString(Boolean) | |
| ToString(Int16) | |
| ToString(Guid) | |
| ToString(Double) | |
| ToString(Decimal) | |
| ToString(DateTimeOffset) |
将提供的 DateTimeOffset 转换为 String。Converts the supplied DateTimeOffset to a String. |
| ToString(DateTime) |
已过时。
|
| ToString(Char) | |
| ToString(Byte) | |
| ToString(Int32) |
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
要转换的值。The value to convert.
返回
Single 的字符串表示形式。A string representation of the Single.
注解
如果 value 为 PositiveInfinity 或 NegativeInfinity,则此方法将分别返回 STRING INF 或-inf。If value is Single.PositiveInfinity or Single.NegativeInfinity, this method returns the string INF or -INF respectively.
另请参阅
适用于
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
要转换的值。The value to convert.
返回
TimeSpan 的字符串表示形式。A string representation of the TimeSpan.
适用于
ToString(UInt16)
重要
此 API 不符合 CLS。
public:
static System::String ^ ToString(System::UInt16 value);
[System.CLSCompliant(false)]
public static string ToString (ushort value);
public static string ToString (ushort value);
[<System.CLSCompliant(false)>]
static member ToString : uint16 -> string
static member ToString : uint16 -> string
Public Shared Function ToString (value As UShort) As String
参数
- value
- UInt16
要转换的值。The value to convert.
返回
UInt16 的字符串表示形式。A string representation of the 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
要转换的值。The value to convert.
返回
UInt32 的字符串表示形式。A string representation of the UInt32.
- 属性
适用于
ToString(DateTimeOffset, String)
将提供的 DateTimeOffset 转换为指定格式的 String。Converts the supplied DateTimeOffset to a String in the specified format.
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。The DateTimeOffset to be converted.
- format
- String
s 转换为的格式。The format to which s is converted. 该格式参数可以是 XML DateTime 类型的 W3C 建议的任何子集。The format parameter can be any subset of the W3C Recommendation for the XML dateTime type. (有关详细信息,请参阅 XML 架构规范的 dateTime 部分。)(For more information, see the dateTime section of the XML Schema specification..)
返回
提供的 DateTimeOffset 的指定格式的 String 表示形式。A String representation in the specified format of the supplied DateTimeOffset.
示例
下面的示例将 DateTimeOffset 当前时间的表示形式转换为 String 指定格式的。The following example converts a DateTimeOffset representation of the current time to a String in the specified format.
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
要转换的值。The value to convert.
- format
- String
定义如何显示转换的字符串的格式结构。The format structure that defines how to display the converted string. 有效格式包括“yyyy-MM-ddTHH:mm:sszzzzzz”及其子集。Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.
返回
指定格式的 DateTime 的字符串表示形式。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();
}
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。Converts the DateTime to a String using the XmlDateTimeSerializationMode specified.
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 值。One of the XmlDateTimeSerializationMode values that specify how to treat the DateTime value.
返回
String 的等效 DateTime。A String equivalent of the DateTime.
例外
dateTimeOption 值无效。The dateTimeOption value is not valid.
value 或 dateTimeOption 值为 null。The value or dateTimeOption value is 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
要转换的值。The value to convert.
返回
SByte 的字符串表示形式。A string representation of the 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
要转换的值。The value to convert.
返回
UInt64 的字符串表示形式。A string representation of the 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
要转换的值。The value to convert.
返回
Boolean 的字符串表示形式,即“true”或“false”。A string representation of the Boolean, that is, "true" or "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
要转换的值。The value to convert.
返回
Int16 的字符串表示形式。A string representation of the Int16.
示例
下面的示例将数据类型转换为字符串,然后将该信息写入控制台。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();
}
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
要转换的值。The value to convert.
返回
Guid 的字符串表示形式。A string representation of the 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
要转换的值。The value to convert.
返回
Double 的字符串表示形式。A string representation of the Double.
示例
下面的示例将数据类型转换为字符串,然后将该信息写入控制台。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();
}
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 为 PositiveInfinity 或 NegativeInfinity,则此方法将分别返回 STRING INF 或-inf。If value is Double.PositiveInfinity or Double.NegativeInfinity, this method returns the string INF or -INF respectively.
另请参阅
适用于
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
要转换的值。The value to convert.
返回
Decimal 的字符串表示形式。A string representation of the Decimal.
适用于
ToString(DateTimeOffset)
将提供的 DateTimeOffset 转换为 String。Converts the supplied DateTimeOffset to a 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。The DateTimeOffset to be converted.
返回
提供的 DateTimeOffset 的 String 表示形式。A String representation of the supplied DateTimeOffset.
示例
下面的示例将 DateTimeOffset 当前时间的表示形式转换为 String 。The following example converts a DateTimeOffset representation of the current time to 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 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
public:
static System::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 takes in XmlDateTimeSerializationMode")>]
static member ToString : DateTime -> string
static member ToString : DateTime -> string
Public Shared Function ToString (value As DateTime) As String
参数
- value
- DateTime
要转换的值。The value to convert.
返回
DateTime 的字符串表示形式,格式为 yyyy-MM-ddTHH:mm:ss,其中“T”是常数文本。A string representation of the DateTime in the format yyyy-MM-ddTHH:mm:ss where 'T' is a constant literal.
- 属性
注解
备注
此 XmlConvert.ToString(DateTime) 方法在 .NET Framework 的2.0 版本中已过时,已替换为 XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode) 方法。The XmlConvert.ToString(DateTime) method is obsolete in the 2.0 version of the .NET Framework and has been replaced with the XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode) method.
建议的模式为 RoundtripKind 。The suggested mode is RoundtripKind. 如果应使用完全匹配,请使用 XmlConvert.ToString(DateTime, String) 格式字符串 yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz 。If an exact match is expected, please use XmlConvert.ToString(DateTime, String) with the format 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
要转换的值。The value to convert.
返回
Char 的字符串表示形式。A string representation of the 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
要转换的值。The value to convert.
返回
Byte 的字符串表示形式。A string representation of the Byte.