Upraviť

Zdieľať cez


Conversion of XML Data Types

The majority of the methods found in an XmlConvert class are used to convert data between strings and strongly typed formats. Methods are locale independent. This means that they do not take into account any locale settings when doing conversion.

Reading String as types

The following sample reads a string and converts it to a DateTime type.

Given the following XML input:

Input

<Element>2001-02-27T11:13:23</Element>  

This code converts the string to the DateTime format:

reader.ReadStartElement()  
Dim vDateTime As DateTime = XmlConvert.ToDateTime(reader.ReadString())  
Console.WriteLine(vDateTime)  
reader.ReadStartElement();  
DateTime vDateTime = XmlConvert.ToDateTime(reader.ReadString());  
Console.WriteLine(vDateTime);  

Writing Strings as types

The following sample reads an Int32 and converts it to a string.

Given the following XML input:

Input

<TestInt32>-2147483648</TestInt32>  

This code converts the Int32 into a String:

Dim vInt32 As Int32 = -2147483648  
writer.WriteElementString("TestInt32", XmlConvert.ToString(vInt32))  
Int32 vInt32=-2147483648;  
writer.WriteElementString("TestInt32",XmlConvert.ToString(vInt32));  

See also