แก้ไข

แชร์ผ่าน


Convert strings to .NET data types

If you want to convert a string to a .NET data type, use the XmlConvert method that fits the application requirements. For a list of all conversion methods available in the XmlConvert class, see XmlConvert.

The string returned from the ToString method is a string version of the data that is passed in. Additionally, there are several .NET types that convert using the XmlConvert class yet they do not use the methods in the System.Convert class. The XmlConvert class follows the XML Schema (XSD) data type specification and has a data type that the XmlConvert can map to.

The following table lists .NET data types and the string types that are returned using XML Schema (XSD) data type mapping. These .NET types cannot be processed using System.Convert.

.NET type String returned
Boolean "true", "false"
Single.PositiveInfinity "INF"
Single.NegativeInfinity "-INF"
Double.PositiveInfinity "INF"
Double.NegativeInfinity "-INF"
DateTime Format is "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.
Timespan Format is PnYnMnTnHnMnS that is, P2Y10M15DT10H30M20S is a duration of 2 years, 10 months, 15 days, 10 hours, 30 minutes, and 20 seconds.

Note

If converting any of the .NET types listed in the table to a string using the ToString method, the returned string is not the base type, but the XML Schema (XSD) string type.

The DateTime and Timespan value type differs in that a DateTime represents an instant in time, whereas a TimeSpan represents a time interval. The DateTime and Timespan formats are specified in the XML Schema (XSD) data types specification. For example:

Dim writer As New XmlTextWriter("myfile.xml", Nothing)  
Dim [date] As New DateTime(2001, 8, 4)  
writer.WriteElementString("Date", XmlConvert.ToString([date]))  
XmlTextWriter writer = new XmlTextWriter("myfile.xml", null);  
DateTime date = new DateTime (2001, 08, 04);  
writer.WriteElementString("Date", XmlConvert.ToString(date));  

Output

<Date>2001-08-04T00:00:00</Date>.

The following code converts an integer to a string:

Dim writer As New XmlTextWriter("myfile.xml", Nothing)  
Dim value As Int32 = 200  
writer.WriteElementString("Number", XmlConvert.ToString(value))  
XmlTextWriter writer = new XmlTextWriter("myfile.xml", null);  
Int32 value = 200;  
writer.WriteElementString("Number", XmlConvert.ToString(value));  

Output

<Number>200</Number>

However, if you are converting a string to Boolean, Single, or Double, the .NET type that's returned is not the same as the type returned when using the System.Convert class.

String to Boolean

The following table shows what type is generated for the given input strings, when converting a string to Boolean using the ToBoolean method.

Valid string input parameter .NET output type
"true" Boolean.True
"1" Boolean.True
"false" Boolean.False
"0" Boolean.False

For example, given the following XML:

Input

<Boolean>true</Boolean>  
<Boolean>1</Boolean>

Both can be understood by the following code, and bvalue is System.Boolean.True:

Dim bvalue As Boolean = _  
   XmlConvert.ToBoolean(reader.ReadElementString())  
Console.WriteLine(bvalue)  
Boolean bvalue = XmlConvert.ToBoolean(reader.ReadElementString());  
Console.WriteLine(bvalue);  

String to Single

The following table shows what type is generated for the given input strings, when converting a string to a Single using the ToSingle method.

Valid string input parameter .NET output type
"INF" Single.PositiveInfinity
"-INF" Single.NegativeInfinity

String to Double

The following table shows what type is generated for the given input strings, when converting a string to a Single using the ToDouble method.

Valid string input parameter .NET output type
"INF" Double.PositiveInfinity
"-INF" Double.NegativeInfinity

The following code writes <Infinity>INF</Infinity>:

Dim value As Double = Double.PositiveInfinity  
writer.WriteElementString("Infinity", XmlConvert.ToString(value))  
Double value = Double.PositiveInfinity;  
writer.WriteElementString("Infinity", XmlConvert.ToString(value));  

See also