Use xmlconvert for XML data

Source: Internet
Author: User

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.

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.