Export (0) Print
Expand All

Conversion of XML Data Types

The majority of the methods found in an XmlConvert class are used to convert data between strings and strongly-typed formats. Methods are locale independent. This means that they do not take into account any locale settings when doing conversion. There is a fixed conversion format, which is described and defined by the XML Schema (XSD) data type specification, and documented in the Data Type Support between XML Schema (XSD) Types and .NET Framework Types topic.

Reading String as types

The following sample reads a string and converts it to a DateTime type.

Given the following XML input:

Input

<Element>2001-02-27T11:13:23</Element>

This code converts the string to the DateTime format:

reader.ReadStartElement()
Dim vDateTime As DateTime = XmlConvert.ToDateTime(reader.ReadString())
Console.WriteLine(vDateTime)
[C#]
reader.ReadStartElement();
DateTime vDateTime = XmlConvert.ToDateTime(reader.ReadString());
Console.WriteLine(vDateTime);

Writing Strings as types

The following sample reads an Int32 and converts it to a string.

Given the following XML input:

Input

<TestInt32>-2147483648</TestInt32>

This code converts the Int32 into a String:

Dim vInt32 As Int32 = -2147483648
writer.WriteElementString("TestInt32", XmlConvert.ToString(vInt32))
[C#]
Int32 vInt32=-2147483648;
writer.WriteElementString("TestInt32",XmlConvert.ToString(vInt32));

See Also

Converting Strings to .NET Framework Data Types | Converting .NET Framework Types to Strings

Show:
© 2015 Microsoft