Home  >  Article  >  Backend Development  >  Using XMLConvert with XML data

Using XMLConvert with XML data

黄舟
黄舟Original
2017-03-03 17:14:281487browse

All data in xml files are stored as strings. When a program loads an XML file, it usually needs to convert the data into a type more suitable for the program.

For example, assuming that the order shipping date exists in an XML file, the program that uses the file needs to convert the data represented by the string into a DateTime object. VB.NET provides the XMLConvert class to assist in this work, converting XML into strongly typed .NET data.

XMLConvert is located in the System.XML namespace. All its methods and properties are shared, so you can access them without instantiating it. It includes methods for converting XML strings into dates, doubles, booleans, and other data types.

Take 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>
    <Double>1234.56</Double>
    <Date>2003-01-01/</Date>
</Data>

This code looks for a file named Convert.xml in the C:\Temp directory. XML file:

Dim xmlDoc As New System.Xml.XmlDocument()
xmlDoc.Load("c:\temp\Convert.xml")
Dim newString As StringnewString = xmlDoc.SelectSingleNode("//String").InnerTextDebug.WriteLine(newString)
Dim newInteger As IntegernewInteger = System.Xml.XmlConvert.ToInt32( _    xmlDoc.SelectSingleNode("//Integer").InnerText)Debug.WriteLine(newInteger)
Dim newDouble As DoublenewDouble = System.Xml.XmlConvert.ToDouble( _    xmlDoc.SelectSingleNode("//Double").InnerText)Debug.WriteLine(newDouble)
Dim newDate As DateTimenewDate = System.Xml.XmlConvert.ToDateTime( _    xmlDoc.SelectSingleNode("//Date").InnerText)Debug.WriteLine(newDate)

All conversion methods are based on the data types defined by 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

The above is the content of using XMLConvert for XML data. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn