Conversion of XML Data Types
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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.

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();
DateTime vDateTime = XmlConvert.ToDateTime(reader.ReadString());
Console.WriteLine(vDateTime);

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:

Int32 vInt32=-2147483648;
writer.WriteElementString("TestInt32",XmlConvert.ToString(vInt32));
Show:
© 2016 Microsoft