XmlConvert.ToString Método

Definição

Converte dados fortemente tipado em uma representação equivalente String .

Sobrecargas

Nome Description
ToString(Single)

Converte em Single um String.

ToString(TimeSpan)

Converte em TimeSpan um String.

ToString(UInt16)

Converte em UInt16 um String.

ToString(UInt32)

Converte em UInt32 um String.

ToString(DateTimeOffset, String)

Converte o fornecido DateTimeOffset em um String no formato especificado.

ToString(DateTime, String)

Converte em DateTime um String.

ToString(DateTime, XmlDateTimeSerializationMode)

Converte em DateTime um String usando o XmlDateTimeSerializationMode especificado.

ToString(SByte)

Converte em SByte um String.

ToString(UInt64)

Converte em UInt64 um String.

ToString(Int64)

Converte em Int64 um String.

ToString(Boolean)

Converte em Boolean um String.

ToString(Int16)

Converte em Int16 um String.

ToString(Guid)

Converte em Guid um String.

ToString(Double)

Converte em Double um String.

ToString(Decimal)

Converte em Decimal um String.

ToString(DateTimeOffset)

Converte o fornecido DateTimeOffset em um String.

ToString(DateTime)
Obsoleto.

Converte em DateTime um String.

ToString(Char)

Converte em Char um String.

ToString(Byte)

Converte em Byte um String.

ToString(Int32)

Converte em Int32 um String.

ToString(Single)

Converte em Single um String.

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

Parâmetros

value
Single

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Single.

Comentários

Se value for Single.PositiveInfinity ou Single.NegativeInfinity, esse método retornará a cadeia de caracteres INF ou -INF respectivamente.

Confira também

Aplica-se a

ToString(TimeSpan)

Converte em TimeSpan um String.

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

Parâmetros

value
TimeSpan

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do TimeSpan.

Aplica-se a

ToString(UInt16)

Importante

Esta API não está em conformidade com CLS.

Converte em UInt16 um String.

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

Parâmetros

value
UInt16

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do UInt16.

Atributos

Aplica-se a

ToString(UInt32)

Importante

Esta API não está em conformidade com CLS.

Converte em UInt32 um String.

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

Parâmetros

value
UInt32

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do UInt32.

Atributos

Aplica-se a

ToString(DateTimeOffset, String)

Converte o fornecido DateTimeOffset em um String no formato especificado.

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

Parâmetros

value
DateTimeOffset

O DateTimeOffset a ser convertido.

format
String

O formato no qual value é convertido. O parâmetro de formato pode ser qualquer subconjunto da Recomendação W3C para o tipo XML dateTime. (Para obter mais informações, consulte a seção dateTime da especificação de esquema XML..)

Retornos

Uma String representação no formato especificado do fornecido DateTimeOffset.

Exemplos

O exemplo a seguir converte uma DateTimeOffset representação da hora atual em um String no formato especificado.

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

Aplica-se a

ToString(DateTime, String)

Converte em DateTime um 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

Parâmetros

value
DateTime

O valor a converter.

format
String

A estrutura de formato que define como exibir a cadeia de caracteres convertida. Os formatos válidos incluem "yyyy-MM-ddTHH:mm:sszzzzzz" e seus subconjuntos.

Retornos

Uma representação de cadeia de caracteres do DateTime formato especificado.

Exemplos

O exemplo a seguir converte tipos de dados em cadeia de caracteres e grava as informações no console.

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

Aplica-se a

ToString(DateTime, XmlDateTimeSerializationMode)

Converte em DateTime um String usando o XmlDateTimeSerializationMode especificado.

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

Parâmetros

value
DateTime

O DateTime valor a converter.

dateTimeOption
XmlDateTimeSerializationMode

Um dos XmlDateTimeSerializationMode valores que especificam como tratar o DateTime valor.

Retornos

Um String equivalente do DateTime.

Exceções

O dateTimeOption valor não é válido.

O value valor ou o dateTimeOption valor é null.

Aplica-se a

ToString(SByte)

Importante

Esta API não está em conformidade com CLS.

Converte em SByte um String.

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

Parâmetros

value
SByte

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do SByte.

Atributos

Aplica-se a

ToString(UInt64)

Importante

Esta API não está em conformidade com CLS.

Converte em UInt64 um String.

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

Parâmetros

value
UInt64

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do UInt64.

Atributos

Aplica-se a

ToString(Int64)

Converte em Int64 um String.

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

Parâmetros

value
Int64

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Int64.

Aplica-se a

ToString(Boolean)

Converte em Boolean um String.

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

Parâmetros

value
Boolean

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Boolean, ou seja, "true" ou "false".

Aplica-se a

ToString(Int16)

Converte em Int16 um String.

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

Parâmetros

value
Int16

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Int16.

Exemplos

O exemplo a seguir converte tipos de dados em cadeia de caracteres e grava as informações no console.

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

Aplica-se a

ToString(Guid)

Converte em Guid um String.

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

Parâmetros

value
Guid

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Guid.

Aplica-se a

ToString(Double)

Converte em Double um String.

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

Parâmetros

value
Double

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Double.

Exemplos

O exemplo a seguir converte tipos de dados em cadeia de caracteres e grava as informações no console.

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

Comentários

Se value for Double.PositiveInfinity ou Double.NegativeInfinity, esse método retornará a cadeia de caracteres INF ou -INF respectivamente.

Confira também

Aplica-se a

ToString(Decimal)

Converte em Decimal um String.

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

Parâmetros

value
Decimal

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Decimal.

Aplica-se a

ToString(DateTimeOffset)

Converte o fornecido DateTimeOffset em um 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

Parâmetros

value
DateTimeOffset

O DateTimeOffset a ser convertido.

Retornos

Uma String representação do fornecido DateTimeOffset.

Exemplos

O exemplo a seguir converte uma DateTimeOffset representação da hora atual em um 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

Aplica-se a

ToString(DateTime)

Cuidado

Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode

Converte em DateTime um String.

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

Parâmetros

value
DateTime

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do DateTime formato yyyy-MM-ddTHH:mm:ss em que 'T' é um literal constante.

Atributos

Comentários

Note

O método XmlConvert.ToString(DateTime) está obsoleto na versão 2.0 do .NET Framework e foi substituído pelo método XmlConvert.ToString(DateTime, XmlDateTimeSerializationMode). O modo sugerido é RoundtripKind. Se uma correspondência exata for esperada, use XmlConvert.ToString(DateTime, String) com a cadeia de caracteres de formato yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz.

Aplica-se a

ToString(Char)

Converte em Char um String.

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

Parâmetros

value
Char

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Char.

Aplica-se a

ToString(Byte)

Converte em Byte um String.

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

Parâmetros

value
Byte

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Byte.

Aplica-se a

ToString(Int32)

Converte em Int32 um String.

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

Parâmetros

value
Int32

O valor a converter.

Retornos

Uma representação de cadeia de caracteres do Int32.

Aplica-se a