Merge pull request #554 from deplinenoise/ppc_fixes
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XUtil.cs
index 1472af6002f4328be5ceb3e9e6807b0b31b951ec..69cb484b23ed49c47b3e3b6a1895c2fec40dcb51 100644 (file)
@@ -27,6 +27,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Globalization;
 using System.IO;
 using System.Text;
 using System.Xml;
@@ -40,7 +41,20 @@ namespace System.Xml.Linq
                public const string XmlnsNamespace =
                        "http://www.w3.org/2000/xmlns/";
 
-               // FIXME: implement
+               public static bool ConvertToBoolean (string s)
+               {
+                       return XmlConvert.ToBoolean (s.ToLower (CultureInfo.InvariantCulture));
+               }
+
+               public static DateTime ToDateTime (string s)
+               {
+                       try {
+                               return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
+                       } catch {
+                               return DateTime.Parse (s);
+                       }
+               }
+
                public static string ToString (object o)
                {
                        if (o == null)
@@ -51,10 +65,20 @@ namespace System.Xml.Linq
                                return (string) o;
                        case TypeCode.DateTime:
                                return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
+                       case TypeCode.Decimal:
+                               return ((decimal) o).ToString (CultureInfo.InvariantCulture);
+                       case TypeCode.Double:
+                               return ((double) o).ToString ("r", CultureInfo.InvariantCulture);
+                       case TypeCode.Single:
+                               return ((float) o).ToString ("r", CultureInfo.InvariantCulture);
                        case TypeCode.Boolean:
                                // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
                                return o.ToString().ToLower();
                        default:
+                               if (o is TimeSpan)
+                                       return XmlConvert.ToString ((TimeSpan) o);
+                               if (o is DateTimeOffset)
+                                       return XmlConvert.ToString ((DateTimeOffset) o);
                                return o.ToString ();
                        }
                }
@@ -94,7 +118,7 @@ namespace System.Xml.Linq
                        else if (o is string)
                                return new XText ((string) o);
                        else
-                               return new XText (o.ToString ());
+                               return new XText (ToString (o));
                }
 
                public static object GetDetachedObject (XObject child)