Exemple #1
0
        private static XmlSchemaSet CreateValidationFailureSchema()
        {
            // Remove support for an attribute that's used by the sample XML, which should cause a validation error.
            string       failingSchemaText    = schemaText.Replace("<xs:attribute name='empty' type='xs:string' use='required' />", "");
            XElement     failingSchemaElement = XElement.Parse(failingSchemaText, LoadOptions.SetLineInfo);
            XmlSchemaSet schema = XmlUtility.CreateSchemaSet(new[] { failingSchemaElement });

            return(schema);
        }
Exemple #2
0
        public void ValidateTest()
        {
            XmlSchemaSet schema = XmlUtility.CreateSchemaSet(new[] { schemaElement });
            var          errors = xmlElement.Validate(schema);

            Assert.AreEqual(0, errors.Count);

            schema = CreateValidationFailureSchema();
            errors = xmlElement.Validate(schema);
            Assert.AreEqual(1, errors.Count);
        }
Exemple #3
0
        public void CreateSchemaTest()
        {
            XmlSchemaSet schema = XmlUtility.CreateSchemaSet(new[] { schemaElement });

            Assert.IsNotNull(schema);

            // Switch use='required' to use='unknown', which is not a supported XSD attribute value.
            string   badSchemaText    = schemaText.Replace("<xs:attribute name='empty' type='xs:string' use='required' />", "<xs:attribute name='empty' type='xs:string' use='unknown' />");
            XElement badSchemaElement = XElement.Parse(badSchemaText, LoadOptions.SetLineInfo);

            List <ValidationEventArgs> errors = new();

            schema = XmlUtility.CreateSchemaSet(new[] { badSchemaElement }, errors);
            Assert.IsNotNull(schema);
            Assert.AreEqual(1, errors.Count);
        }
Exemple #4
0
        public void RequireValidationTest()
        {
            XmlSchemaSet schema = XmlUtility.CreateSchemaSet(new[] { schemaElement });

            xmlElement.RequireValidation(schema);

            try
            {
                schema = CreateValidationFailureSchema();
                xmlElement.RequireValidation(schema);
                Assert.Fail("We should never get past RequireValidation");
            }
            catch (XmlSchemaValidationException ex)
            {
                Assert.IsTrue(!string.IsNullOrEmpty(ex.Message));
            }
        }