Apply XMLConvert to XML data

Source: Internet
Author: User
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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.