Exemple #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>
        /// xpathitem.ValueAs<int>(nsResolver);
        /// </example>
        /// </summary>
        public static T ValueAs <T>(this XPathItem xpathitem, System.Xml.IXmlNamespaceResolver nsResolver)
        {
            if (xpathitem == null)
            {
                throw new ArgumentNullException("xpathitem");
            }

            return((T)xpathitem.ValueAs(typeof(T), nsResolver));
        }
        public bool Match(XPathItem item, XPath2Context context)
        {
            switch (TypeCode)
            {
            case XmlTypeCode.None:
                return(false);

            case XmlTypeCode.Item:
                return(true);

            case XmlTypeCode.Node:
                return(item.IsNode);

            case XmlTypeCode.AnyAtomicType:
                return(!item.IsNode);

            case XmlTypeCode.UntypedAtomic:
                return(!item.IsNode && item.GetSchemaType() == XmlSchema.UntypedAtomic);

            case XmlTypeCode.Document:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    if (nav.NodeType == XPathNodeType.Root)
                    {
                        XPathNavigator cur = nav.Clone();
                        if (SchemaElement == null)
                        {
                            if (cur.MoveToChild(XPathNodeType.Element) && MatchName(cur, context))
                            {
                                if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                                {
                                    return(true);
                                }
                                IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                                if (schemaInfo != null)
                                {
                                    if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                    XmlSchemaDerivationMethod.Empty))
                                    {
                                        return(!schemaInfo.IsNil || Nillable);
                                    }
                                }
                                else
                                {
                                    return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                       XmlSchemaDerivationMethod.Empty));
                                }
                            }
                        }
                        else
                        {
                            if (!cur.MoveToChild(XPathNodeType.Element))
                            {
                                return(false);
                            }
                            IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                            if (schemaInfo != null)
                            {
                                return(schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName);
                            }
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.Element:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null && nav.NodeType == XPathNodeType.Element)
                {
                    if (SchemaElement == null)
                    {
                        if (MatchName(nav, context))
                        {
                            if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                            {
                                return(true);
                            }
                            IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                            if (schemaInfo != null)
                            {
                                if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                XmlSchemaDerivationMethod.Empty))
                                {
                                    return(!schemaInfo.IsNil || Nillable);
                                }
                            }
                            else
                            {
                                return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                        }
                    }
                    else
                    {
                        IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                        if (schemaInfo != null)
                        {
                            return(schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName);
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.Attribute:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null && nav.NodeType == XPathNodeType.Attribute)
                {
                    if (SchemaAttribute == null)
                    {
                        if (MatchName(nav, context))
                        {
                            if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                            {
                                return(true);
                            }
                            IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                            if (schemaInfo == null)
                            {
                                return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                            else
                            {
                                return(XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                        }
                    }
                    else
                    {
                        IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                        if (schemaInfo != null)
                        {
                            return(schemaInfo.SchemaAttribute.QualifiedName == SchemaAttribute.QualifiedName);
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.ProcessingInstruction:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.ProcessingInstruction &&
                           (NameTest.IsNameWildcard || NameTest.Name == nav.Name));
                }
            }
            break;

            case XmlTypeCode.Comment:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.Comment);
                }
            }
            break;

            case XmlTypeCode.Text:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.Text ||
                           nav.NodeType == XPathNodeType.SignificantWhitespace);
                }
            }
            break;

            case XmlTypeCode.PositiveInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) > 0);
                }
                break;

            case XmlTypeCode.NegativeInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) < 0);
                }
                break;

            case XmlTypeCode.NonPositiveInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) <= 0);
                }
                break;

            case XmlTypeCode.NonNegativeInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) >= 0);

                case XmlTypeCode.UnsignedByte:
                case XmlTypeCode.UnsignedShort:
                case XmlTypeCode.UnsignedInt:
                case XmlTypeCode.UnsignedLong:
                    return(true);
                }
                break;

            case XmlTypeCode.Integer:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                case XmlTypeCode.UnsignedByte:
                case XmlTypeCode.UnsignedShort:
                case XmlTypeCode.UnsignedInt:
                case XmlTypeCode.UnsignedLong:
                    return(true);

                case XmlTypeCode.Decimal:
                    decimal value = (decimal)item.ValueAs(typeof(Decimal));
                    return(value == Math.Truncate(value));
                }
                break;

            case XmlTypeCode.Entity:
                return((item.GetSchemaType().TypeCode == XmlTypeCode.String) ||
                       (item.GetSchemaType().TypeCode == XmlTypeCode.Entity));

            default:
            {
                if (item.XmlType != null)
                {
                    return(XmlSchemaType.IsDerivedFrom(item.XmlType, SchemaType, XmlSchemaDerivationMethod.Empty));
                }
            }
            break;
            }
            return(false);
        }
 private T ValueOrDefault <T>(XPathItem item) => item.Value == String.Empty ? default(T) : (T)item.ValueAs(typeof(T));