System.Xml.XPath.XPathNavigator.CheckValidity C# (CSharp) Method

CheckValidity() public method

public CheckValidity ( XmlSchemaSet schemas, ValidationEventHandler validationEventHandler ) : bool
schemas System.Xml.Schema.XmlSchemaSet
validationEventHandler ValidationEventHandler
return bool
        public virtual bool CheckValidity(XmlSchemaSet schemas, ValidationEventHandler validationEventHandler)
        {
            IXmlSchemaInfo schemaInfo;
            XmlSchemaType schemaType = null;
            XmlSchemaElement schemaElement = null;
            XmlSchemaAttribute schemaAttribute = null;

            switch (NodeType)
            {
                case XPathNodeType.Root:
                    if (schemas == null)
                    {
                        throw new InvalidOperationException(SR.XPathDocument_MissingSchemas);
                    }
                    schemaType = null;
                    break;
                case XPathNodeType.Element:
                    if (schemas == null)
                    {
                        throw new InvalidOperationException(SR.XPathDocument_MissingSchemas);
                    }
                    schemaInfo = SchemaInfo;
                    if (schemaInfo != null)
                    {
                        schemaType = schemaInfo.SchemaType;
                        schemaElement = schemaInfo.SchemaElement;
                    }
                    if (schemaType == null
                        && schemaElement == null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.XPathDocument_NotEnoughSchemaInfo, null));
                    }
                    break;
                case XPathNodeType.Attribute:
                    if (schemas == null)
                    {
                        throw new InvalidOperationException(SR.XPathDocument_MissingSchemas);
                    }
                    schemaInfo = SchemaInfo;
                    if (schemaInfo != null)
                    {
                        schemaType = schemaInfo.SchemaType;
                        schemaAttribute = schemaInfo.SchemaAttribute;
                    }
                    if (schemaType == null
                        && schemaAttribute == null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.XPathDocument_NotEnoughSchemaInfo, null));
                    }
                    break;
                default:
                    throw new InvalidOperationException(SR.Format(SR.XPathDocument_ValidateInvalidNodeType, null));
            }

            Debug.Assert(schemaType != null || this.NodeType == XPathNodeType.Root, "schemaType != null  || this.NodeType == XPathNodeType.Root");

            XmlReader reader = CreateReader();

            CheckValidityHelper validityTracker = new CheckValidityHelper(validationEventHandler, reader as XPathNavigatorReader);
            validationEventHandler = new ValidationEventHandler(validityTracker.ValidationCallback);
            XmlReader validatingReader = GetValidatingReader(reader, schemas, validationEventHandler, schemaType, schemaElement, schemaAttribute);

            while (validatingReader.Read())
                ;

            return validityTracker.IsValid;
        }

Usage Example

Example #1
0
 private void ValidateXPathNavigator(XPathNavigator nav, XmlSchemaSet schemaSet)
 {
     _output.WriteLine(nav.CheckValidity(schemaSet, OnValidationEvent) ? "Validation succeeded." : "Validation failed.");
 }