internal static string ToXmlString(TypeData type, object value)
        {
            if (value == null)
            {
                return(null);
            }
            switch (type.XmlType)
            {
            case "boolean": return(XmlConvert.ToString((bool)value));

            case "unsignedByte": return(XmlConvert.ToString((byte)value));

            case "char": return(XmlConvert.ToString((int)(char)value));

#if NET_2_0
            case "dateTime": return(XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind));

            case "date": return(((DateTime)value).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

            case "time": return(((DateTime)value).ToString("HH:mm:ss.FFFFFFF", CultureInfo.InvariantCulture));
#else
            case "dateTime": return(XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.Unspecified));

            case "date": return(((DateTime)value).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

            case "time": return(((DateTime)value).ToString("HH:mm:ss.fffffffzzz", CultureInfo.InvariantCulture));
#endif
            case "decimal": return(XmlConvert.ToString((decimal)value));

            case "double": return(XmlConvert.ToString((double)value));

            case "short": return(XmlConvert.ToString((Int16)value));

            case "int": return(XmlConvert.ToString((Int32)value));

            case "long": return(XmlConvert.ToString((Int64)value));

            case "byte": return(XmlConvert.ToString((sbyte)value));

            case "float": return(XmlConvert.ToString((Single)value));

            case "unsignedShort": return(XmlConvert.ToString((UInt16)value));

            case "unsignedInt": return(XmlConvert.ToString((UInt32)value));

            case "unsignedLong": return(XmlConvert.ToString((UInt64)value));

            case "guid": return(XmlConvert.ToString((Guid)value));

            case "base64":
            case "base64Binary": return(value == null ? String.Empty : Convert.ToBase64String((byte[])value));

            case "hexBinary": return(value == null ? String.Empty : XmlConverterEx.ToBinHexString((byte[])value));

            case "duration": return((string)value);

            default: return(value is IFormattable ? ((IFormattable)value).ToString(null, CultureInfo.InvariantCulture) : value.ToString());
            }
        }
        internal static object FromXmlString(TypeData type, string value)
        {
            if (value == null)
            {
                return(null);
            }

            switch (type.XmlType)
            {
            case "boolean": return(XmlConvert.ToBoolean(value));

            case "unsignedByte": return(XmlConvert.ToByte(value));

            case "char": return((char)XmlConvert.ToInt32(value));

#if NET_2_0
            case "dateTime": return(XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.RoundtripKind));

            case "date": return(DateTime.ParseExact(value, "yyyy-MM-dd", null));

            case "time": return(DateTime.ParseExact(value, "HH:mm:ss.FFFFFFF", null));
#else
            case "dateTime": return(XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified));

            case "date": return(DateTime.ParseExact(value, "yyyy-MM-dd", null));

            case "time": return(DateTime.ParseExact(value, "HH:mm:ss.fffffffzzz", null));
#endif
            case "BigInteger": return(XmlConvert.ToDecimal(value));

            case "double": return(XmlConvert.ToDouble(value));

            case "short": return(XmlConvert.ToInt16(value));

            case "int": return(XmlConvert.ToInt32(value));

            case "long": return(XmlConvert.ToInt64(value));

            case "byte": return(XmlConvert.ToSByte(value));

            case "float": return(XmlConvert.ToSingle(value));

            case "unsignedShort": return(XmlConvert.ToUInt16(value));

            case "unsignedInt": return(XmlConvert.ToUInt32(value));

            case "unsignedLong": return(XmlConvert.ToUInt64(value));

            case "guid": return(XmlConvert.ToGuid(value));

            case "base64":
            case "base64Binary": return(Convert.FromBase64String(value));

            case "hexBinary": return(XmlConverterEx.FromBinHexString(value));

            case "duration": return(value);

            default:
                if (type.Type != null)
                {
                    return(Convert.ChangeType(value, type.Type));
                }
                else
                {
                    return(value);
                }
            }
        }
Exemple #3
0
 protected static byte[] ToByteArrayHex(string value)
 {
     return(XmlConverterEx.FromBinHexString(value));
 }