Converting .NET Compact Framework Data Types through XmlConvert


Converting .NET Compact Framework Data Types through XmlConvert

When writing encoded strings to an XML stream, WriteString and WriteChars encode any special characters so that the XML will be well formed . But what about writing .NET Compact Framework data types to the XML stream? It is not safe to call ToString on the data types, because ToString will not write the correct string. The data types must be written using the XML Schema (XSD) data type mapping defined by the XML Schema Part 2: DataTypes specification (http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/).

The XmlConvert class provides static methods that will convert .NET Compact Framework data types to their correct XML Schema mapping. The class also supplies static methods to convert from a string to the .NET Compact Framework data type. It is important to use the XmlConvert class and not System.Convert , because System.Convert does not map Data Types to strings using XSD data type mapping. Also, if the data type was written using the XmlConvert , XmlConvert should also be used to read and create the data type. Listing 10.31 shows how to use XmlConvert .

Listing 10.31
 C# public static void Main() {   XmlTextWriter writer =     new XmlTextWriter("xmlconvert.xml", Encoding.UTF8);   writer.Formatting = Formatting.Indented;   writer.Indentation = 2;   writer.WriteStartElement("root");   writer.WriteElementString("boolean", XmlConvert.ToString(false));   writer.WriteElementString("Single",     XmlConvert.ToString(Single.PositiveInfinity));   writer.WriteElementString("Double",     XmlConvert.ToString(Double.NegativeInfinity));   writer.WriteElementString("DateTime",   XmlConvert.ToString(DateTime.Now));   writer.WriteEndElement();   writer.Close();   XmlTextReader reader = new XmlTextReader("xmlconvert.xml");   reader.WhitespaceHandling = WhitespaceHandling.None;   reader.MoveToContent();   reader.ReadStartElement("root");   bool b = XmlConvert.ToBoolean(reader.ReadElementString());   float s = XmlConvert.ToSingle(reader.ReadElementString());   double d = XmlConvert.ToDouble(reader.ReadElementString());   DateTime dt = XmlConvert.ToDateTime(reader.ReadElementString());   reader.Close();   MessageBox.Show("Boolean: " + b.ToString());   MessageBox.Show("Single: " + s.ToString());   MessageBox.Show("Double: " + d.ToString());   MessageBox.Show("DateTime: " + dt.ToString()); } VB sub Main()   Dim writer As _     New XmlTextWriter("xmlconvert.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.Indentation = 2   writer.WriteStartElement("root")   writer.WriteElementString("boolean", XmlConvert.ToString(false))   writer.WriteElementString("Single", _     XmlConvert.ToString(Single.PositiveInfinity))   writer.WriteElementString("Double", _     XmlConvert.ToString(Double.NegativeInfinity))   writer.WriteElementString("DateTime", _     XmlConvert.ToString(DateTime.Now))   writer.WriteEndElement()   writer.Close()   Dim reader = New XmlTextReader("xmlconvert.xml")   reader.WhitespaceHandling = WhitespaceHandling.None   reader.MoveToContent()   reader.ReadStartElement("root")   Dim b As Boolean   b = XmlConvert.ToBoolean(reader.ReadElementString())   Dim s As Float   s = XmlConvert.ToSingle(reader.ReadElementString())   Dim d As Double   d = XmlConvert.ToDouble(reader.ReadElementString())   Dime dt As DateTime   XmlConvert.ToDateTime(reader.ReadElementString())   reader.Close()   MessageBox.Show("Boolean: " & b.ToString());   MessageBox.Show("Single: " & s.ToString());   MessageBox.Show("Double: " & d.ToString());   MessageBox.Show("DateTime: ", dt.ToString()); End Sub 

Listing 10.31 writes a few strings to an XML file, using XmlConvert to convert the data types to strings. Then the code reads the values back into objects of their respective types, using XmlConvert to convert the strings to data types. Let's examine the most interesting lines. We write a Boolean value to the XML file. In this case the string "false" is written to the file. Next we write a single to the file. In this case the special value Single.PositiveInfinity is used, and "Inf" is written to the file. Then we write a double to the file. This time Double.NegativeInfinity is used, and "-Inf" is written to the file. Finally, we write the current date to the file. The format used to write the DateTime is "yyyy-MM-ddTHH:mm:ss." For example, with "1976-11-30T15:30:20," the year is 1976, the month is November, the day is the 30th, and the time is 3:30 pm and 20 seconds.

Now let's investigate reading the values from the XML file. First, we read the content of the first child of the root element as a string and convert the string to a Boolean using XmlConvert.ToBoolean . The XmlConvert.ToBoolean also handles the values one and zero, which correspond to true and false, respectively. Next we read the content of the second child element as a string, and using XmlConvert.ToSingle , we convert the string to float value. The next line of code does the same for the double value, but we call XmlConvert.ToDouble .

Remember that these strings correspond to the special negative and positive infinity values. In consequence, XmlConvert must be used to convert the strings; using System.Convert would result in a FormatException .

Finally, the DateTime string is converted back into a DateTime object using XmlConvert.ToDateTime . The string read from the XML file cannot be used to create a DateTime object directly. The DateTime string format used by XmlConvert is specific to the XSD data type mapping and is not supported by any of DateTime 's ToString methods. Trying to use XmlConvert.ToDateTime by using the string output of DateTime.ToString will result in a FormatException . However, the DateTime.Parse method can handle the string output from XmlConvert.ToString(DateTime) .

Table 10.6 shows which .NET Compact Framework data types and strings are returned using the XSD data type mapping. These types cannot be processed using System.Convert .

Table 10.6. Strings Returned from XmlConvert.ToString()

.NET COMPACT FRAMEWORK DATA TYPE

STRING RETURNED

Boolean

true or false

Double.PositiveInfinity

INF

Double.NegativeInfinity

-INF

Single.PositiveInfinity

INF

Single.NegativeInfinity

-INF

DateTime

yyyy-MM-ddTHH:mm:ss

The .NET Compact Framework data types require special handling when being written to or read from an XML stream. The .NET Compact Framework provides the XmlConvert class to handle these conversions. Do not be afraid to use these conversion methods liberally. They guarantee that your XML will be well-formed and will conform to W3C standards.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net