All data in the XML file is stored as strings. When a program loads an XML file, it usually needs to convert the data to a more appropriate program type. For example, if the order shipment date exists in an XML file, the program that applies this file needs
All data in the XML file is stored as strings. When a program loads an XML file, it usually needs to convert the data to a more appropriate program type.
For example, if the order shipment date exists in an XML file, the program that applies the file needs to convert the data represented by the string into a DateTime object. VB. NET provides the XMLConvert class to help with this work, and converts XML into strong type. NET data.
XMLConvert is located in the System. XML namespace. All of its methods and attributes are shared, so you can visit them without having to make them practical. It contains 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 apply the XMLConvert class for type conversion:
Reference content is as follows:
Test
123
1234.56
2003-01-01/
Find 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 scale. You can find more information about the XML Schema Type and. NET in the MSDN Library.