Exemple #1
0
 public virtual float ReadContentAsFloat()
 {
     try {
         return(XQueryConvert.StringToFloat(ReadContentString()));
     } catch (FormatException ex) {
         throw XmlError("Typed value is invalid.", ex);
     }
 }
Exemple #2
0
 public virtual float ReadElementContentAsFloat(string localName, string namespaceURI)
 {
     try {
         return(XQueryConvert.StringToFloat(ReadElementContentAsString(localName, namespaceURI)));
     } catch (FormatException ex) {
         throw XmlError("Typed value is invalid.", ex);
     }
 }
Exemple #3
0
        private object ValueAs(string text, Type type, IXmlNamespaceResolver resolver)
        {
            try {
                if (type == typeof(object))
                {
                    return(text);
                }
                if (type == typeof(XmlQualifiedName))
                {
                    if (resolver != null)
                    {
                        return(XmlQualifiedName.Parse(text, resolver));
                    }
                    else
                    {
                        return(XmlQualifiedName.Parse(text, this));
                    }
                }
                if (type == typeof(DateTimeOffset))
                {
                    return(XmlConvert.ToDateTimeOffset(text));
                }

                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                    return(XQueryConvert.StringToBoolean(text));

                case TypeCode.DateTime:
                    return(XQueryConvert.StringToDateTime(text));

                case TypeCode.Decimal:
                    return(XQueryConvert.StringToDecimal(text));

                case TypeCode.Double:
                    return(XQueryConvert.StringToDouble(text));

                case TypeCode.Int32:
                    return(XQueryConvert.StringToInt(text));

                case TypeCode.Int64:
                    return(XQueryConvert.StringToInteger(text));

                case TypeCode.Single:
                    return(XQueryConvert.StringToFloat(text));

                case TypeCode.String:
                    return(text);
                }
            } catch (Exception ex) {
                throw XmlError(String.Format("Current text value '{0}' is not acceptable for specified type '{1}'. {2}", text, type, ex != null ? ex.Message : String.Empty), ex);
            }
            throw new ArgumentException(String.Format("Specified type '{0}' is not supported.", type));
        }
Exemple #4
0
        private object ValueAs(string text, Type type, IXmlNamespaceResolver resolver, bool isArrayItem)
        {
            try {
                if (type == typeof(object))
                {
                    return(text);
                }
                if (type.IsArray && !isArrayItem)
                {
                    var elemType = type.GetElementType();
                    var sarr     = text.Split((string [])null, StringSplitOptions.RemoveEmptyEntries);
                    var ret      = Array.CreateInstance(elemType, sarr.Length);
                    for (int i = 0; i < ret.Length; i++)
                    {
                        ret.SetValue(ValueAs(sarr [i], elemType, resolver, true), i);
                    }
                    return(ret);
                }

                if (type == typeof(XmlQualifiedName))
                {
                    if (resolver != null)
                    {
                        return(XmlQualifiedName.Parse(text, resolver, true));
                    }
                    else
                    {
                        return(XmlQualifiedName.Parse(text, this, true));
                    }
                }
                if (type == typeof(Uri))
                {
                    return(XmlConvert.ToUri(text));
                }
                if (type == typeof(TimeSpan))
                {
                    return(XmlConvert.ToTimeSpan(text));
                }
                if (type == typeof(DateTimeOffset))
                {
                    return(XmlConvert.ToDateTimeOffset(text));
                }

                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                    return(XQueryConvert.StringToBoolean(text));

                case TypeCode.DateTime:
                    return(XQueryConvert.StringToDateTime(text));

                case TypeCode.Decimal:
                    return(XQueryConvert.StringToDecimal(text));

                case TypeCode.Double:
                    return(XQueryConvert.StringToDouble(text));

                case TypeCode.Int32:
                    return(XQueryConvert.StringToInt(text));

                case TypeCode.Int64:
                    return(XQueryConvert.StringToInteger(text));

                case TypeCode.Single:
                    return(XQueryConvert.StringToFloat(text));

                case TypeCode.String:
                    return(text);
                }
            } catch (Exception ex) {
                throw XmlError(String.Format("Current text value '{0}' is not acceptable for specified type '{1}'. {2}", text, type, ex != null ? ex.Message : String.Empty), ex);
            }
            throw new ArgumentException(String.Format("Specified type '{0}' is not supported.", type));
        }