All data in the XML file is stored as a string. When a program loads an XML file, it is usually necessary to convert the data to a more suitable type for the program.
For example, suppose that the order shipment date is in an XML file, the program using the file needs to convert the data represented by the string into a DateTime object. The XMLConvert class is provided to assist in this work, converting XML into strongly typed .NET data.
XMLConvert is located in the namespace. All its methods and properties are shared, so they can be accessed without instantiating them. It includes methods to convert XML strings to date, doubles, booleans and other data types.
Take the following XML file as an example. We will demonstrate how to use the XMLConvert class to perform type conversion:
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<String>Test</String>
<Integer>123</Integer>
<Double>1234.56</Double>
<Date>2003-01-01/</Date>
</Data>
This code looks for an XML file named in the C:\Temp directory:
Dim xmlDoc As New ()
("c:\temp\")
Dim newString As String
newString = ("//String").InnerText
(newString)
Dim newInteger As Integer
newInteger = .ToInt32( _
("//Integer").InnerText)
(newInteger)
Dim newDouble As Double
newDouble = ( _
("//Double").InnerText)
(newDouble)
Dim newDate As DateTime
newDate = ( _
("//Date").InnerText)
(newDate)
All conversion methods are based on data types defined by XML Schema. The converted XML data must be consistent with the XML Schema standard. You can find more information about XML Schema types and .NET in the MSDN Library.