XmlConvert.ToString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将强类型数据转换为等效的 String 表示形式。
重载
ToString(Single)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
重要
此 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
重要
此 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
将提供的 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。
返回
提供的 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
使用指定的 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
重要
此 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
重要
此 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
将提供的 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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
注意
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)在 .NET Framework的 2.0 版本中已过时,并已替换为 XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode) 方法。
建议的模式为 RoundtripKind。 如果预期完全匹配,请将 与格式字符串 yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz
一XmlConvert.ToString(DateTime, String)起使用。
适用于
ToString(Char)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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)
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
- Source:
- XmlConvert.cs
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 的字符串表示形式。