xml| data
All data in an XML file is stored as a string. When a program loads an XML file, it is often necessary to convert the data to a more appropriate type of program.
For example, if the order ship date exists in an XML file, the program using the file needs to convert the data represented by the string to a DateTime object. Vb. NET provides a XmlConvert class to assist in this effort by converting XML to strongly typed. NET data.
XmlConvert is located in the System.Xml namespace. All of its methods and properties are shared, so you can access them without instantiating them. It includes methods for converting XML strings into other data types such as dates, doubles, and Boolean values.
Take the following XML file for example, we'll show you how to use the XmlConvert class for type conversions:
<?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 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 based on the data type defined by the 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.