Using XmlConvert FOR XML data

Source: Internet
Author: User
Tags date datetime integer net string
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.



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.