System.Xml.DocumentSchemaValidator.Validate C# (CSharp) Méthode

Validate() public méthode

public Validate ( XmlNode nodeToValidate ) : bool
nodeToValidate XmlNode
Résultat bool
        public bool Validate(XmlNode nodeToValidate)
        {
            XmlSchemaObject partialValidationType = null;
            XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;
            Debug.Assert(nodeToValidate.SchemaInfo != null);

            _startNode = nodeToValidate;
            switch (nodeToValidate.NodeType)
            {
                case XmlNodeType.Document:
                    validationFlags |= XmlSchemaValidationFlags.ProcessIdentityConstraints;
                    break;

                case XmlNodeType.DocumentFragment:
                    break;

                case XmlNodeType.Element: //Validate children of this element
                    IXmlSchemaInfo schemaInfo = nodeToValidate.SchemaInfo;
                    XmlSchemaElement schemaElement = schemaInfo.SchemaElement;
                    if (schemaElement != null)
                    {
                        if (!schemaElement.RefName.IsEmpty)
                        { //If it is element ref,
                            partialValidationType = _schemas.GlobalElements[schemaElement.QualifiedName]; //Get Global element with correct Nillable, Default etc
                        }
                        else
                        { //local element
                            partialValidationType = schemaElement;
                        }
                        //Verify that if there was xsi:type, the schemaElement returned has the correct type set
                        Debug.Assert(schemaElement.ElementSchemaType == schemaInfo.SchemaType);
                    }
                    else
                    { //Can be an element that matched xs:any and had xsi:type
                        partialValidationType = schemaInfo.SchemaType;

                        if (partialValidationType == null)
                        { //Validated against xs:any with pc= lax or skip or undeclared / not validated element
                            if (nodeToValidate.ParentNode.NodeType == XmlNodeType.Document)
                            {
                                //If this is the documentElement and it has not been validated at all
                                nodeToValidate = nodeToValidate.ParentNode;
                            }
                            else
                            {
                                partialValidationType = FindSchemaInfo(nodeToValidate as XmlElement);
                                if (partialValidationType == null)
                                {
                                    throw new XmlSchemaValidationException(SR.XmlDocument_NoNodeSchemaInfo, null, nodeToValidate);
                                }
                            }
                        }
                    }
                    break;

                case XmlNodeType.Attribute:
                    if (nodeToValidate.XPNodeType == XPathNodeType.Namespace) goto default;
                    partialValidationType = nodeToValidate.SchemaInfo.SchemaAttribute;
                    if (partialValidationType == null)
                    { //Validated against xs:anyAttribute with pc = lax or skip / undeclared attribute
                        partialValidationType = FindSchemaInfo(nodeToValidate as XmlAttribute);
                        if (partialValidationType == null)
                        {
                            throw new XmlSchemaValidationException(SR.XmlDocument_NoNodeSchemaInfo, null, nodeToValidate);
                        }
                    }
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.XmlDocument_ValidateInvalidNodeType, null));
            }
            _isValid = true;
            CreateValidator(partialValidationType, validationFlags);
            if (_psviAugmentation)
            {
                if (_schemaInfo == null)
                { //Might have created it during FindSchemaInfo
                    _schemaInfo = new XmlSchemaInfo();
                }
                _attributeSchemaInfo = new XmlSchemaInfo();
            }
            ValidateNode(nodeToValidate);
            _validator.EndValidation();
            return _isValid;
        }

Usage Example

 public override bool CheckValidity(XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
 {
     XmlDocument source;
     if (this.source.NodeType == XmlNodeType.Document)
     {
         source = (XmlDocument) this.source;
     }
     else
     {
         source = this.source.OwnerDocument;
         if (schemas != null)
         {
             throw new ArgumentException(Res.GetString("XPathDocument_SchemaSetNotAllowed", (object[]) null));
         }
     }
     if ((schemas == null) && (source != null))
     {
         schemas = source.Schemas;
     }
     if ((schemas == null) || (schemas.Count == 0))
     {
         throw new InvalidOperationException(Res.GetString("XmlDocument_NoSchemaInfo"));
     }
     DocumentSchemaValidator validator = new DocumentSchemaValidator(source, schemas, validationEventHandler) {
         PsviAugmentation = false
     };
     return validator.Validate(this.source);
 }
All Usage Examples Of System.Xml.DocumentSchemaValidator::Validate