All data in the XML file is stored as strings. WhenProgramWhen loading an XML file, you usually need to convert the data to a type that is more suitable for the program.
For example, if the order shipment date exists in an XML file, the program using this file needs to convert the data represented by a string into a datetime object. VB. NET provides the xmlconvert class to help with this work and convert XML into strongly typed. NET data.
Xmlconvert is located in the system. xml namespace. All its methods and attributes are shared, so they can be accessed without being instantiated. It includes methods to convert XML strings into other data types such as date, double precision, and Boolean value.
Taking the following XML file as an example, we will demonstrate how to use the xmlconvert class for type conversion:
<? XML version = "1.0" encoding = "UTF-8"?>
<DATA>
<String> test </string>
<Integer> 123 </Integer>
& Lt; double & gt; 1234.56 & lt;/Double & gt;
<Date> 2003-01-01/</date>
</Data>
This sectionCodeFind the XML file convert. XML in the C: \ Temp directory:
Dim xmldoc as new system. xml. xmldocument ()
Xmldoc. Load ("C: \ temp \ convert. xml ")
Dim newstring as string
Newstring = xmldoc. selectsinglenode ("// string"). innertext
Debug. writeline (newstring)
Dim newinteger as integer
Newinteger = system. xml. xmlconvert. toint32 (_
Xmldoc. selectsinglenode ("// integer"). innertext)
Debug. writeline (newinteger)
Dim newdouble as double
Newdouble = system. xml. xmlconvert. todouble (_
Xmldoc. selectsinglenode ("// double"). innertext)
Debug. writeline (newdouble)
Dim newdate as datetime
Newdate = system. xml. xmlconvert. todatetime (_
Xmldoc. selectsinglenode ("// Date"). innertext)
Debug. writeline (newdate)
All conversion methods are data types defined based on XML schema. The converted XML data must be consistent with the XML schema standard. You can find more information about the XML schema type and. net in the msdn library.