Example #1
0
        /// <summary>
        /// Extends ValueAs so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// xmlatomicvalue.ValueAs&lt;int&gt;(nsResolver);
        /// </example>
        /// </summary>
        public static T ValueAs <T>(this XmlAtomicValue xmlatomicvalue, System.Xml.IXmlNamespaceResolver nsResolver)
        {
            if (xmlatomicvalue == null)
            {
                throw new ArgumentNullException("xmlatomicvalue");
            }

            return((T)xmlatomicvalue.ValueAs(typeof(T), nsResolver));
        }
Example #2
0
        //------------------------------------------------------------------------
        // External type to external type
        //------------------------------------------------------------------------

        internal static XmlAtomicValue ConvertToType(XmlAtomicValue value, XmlQueryType destinationType)
        {
            Debug.Assert(destinationType.IsStrict && destinationType.IsAtomicValue, "Can only convert to strict atomic type.");

            // This conversion matrix should match the one in XmlILVisitor.GetXsltConvertMethod
            switch (destinationType.TypeCode)
            {
            case XmlTypeCode.Boolean:
                switch (value.XmlType.TypeCode)
                {
                case XmlTypeCode.Boolean:
                case XmlTypeCode.Double:
                case XmlTypeCode.String:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToBoolean(value)));
                }
                break;

            case XmlTypeCode.DateTime:
                if (value.XmlType.TypeCode == XmlTypeCode.String)
                {
                    return(new XmlAtomicValue(destinationType.SchemaType, ToDateTime(value.Value)));
                }
                break;

            case XmlTypeCode.Decimal:
                if (value.XmlType.TypeCode == XmlTypeCode.Double)
                {
                    return(new XmlAtomicValue(destinationType.SchemaType, ToDecimal(value.ValueAsDouble)));
                }
                break;

            case XmlTypeCode.Double:
                switch (value.XmlType.TypeCode)
                {
                case XmlTypeCode.Boolean:
                case XmlTypeCode.Double:
                case XmlTypeCode.String:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToDouble(value)));

                case XmlTypeCode.Decimal:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToDouble((decimal)value.ValueAs(DecimalType, null))));

                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToDouble(value.ValueAsLong)));
                }
                break;

            case XmlTypeCode.Int:
            case XmlTypeCode.Long:
                if (value.XmlType.TypeCode == XmlTypeCode.Double)
                {
                    return(new XmlAtomicValue(destinationType.SchemaType, ToLong(value.ValueAsDouble)));
                }
                break;

            case XmlTypeCode.String:
                switch (value.XmlType.TypeCode)
                {
                case XmlTypeCode.Boolean:
                case XmlTypeCode.Double:
                case XmlTypeCode.String:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToString(value)));

                case XmlTypeCode.DateTime:
                    return(new XmlAtomicValue(destinationType.SchemaType, ToString(value.ValueAsDateTime)));
                }
                break;
            }

            Debug.Fail($"Conversion from {value.XmlType.QualifiedName.Name} to {destinationType} is not supported.");
            return(value);
        }