Example #1
0
        /// <summary>
        /// Validates the schema.
        /// </summary>
        public static async Task <List <BuildError> > ValidateSchema(string xml, string filename, CancellationToken token)
        {
            var errors   = new List <BuildError> ();
            var settings = new XmlReaderSettings {
                XmlResolver = new LocalOnlyXmlResolver()
            };

            void validationCallback(object _, ValidationEventArgs args)
            {
                errors.Add(CreateBuildError(args, filename));
            }

            try {
                XmlSchema schema;
                using (var xmlReader = XmlReader.Create(new StringReader(xml))) {
                    schema = XmlSchema.Read(xmlReader, validationCallback);
                }

                var sset = new XmlSchemaSet();
                sset.ValidationEventHandler += validationCallback;

                foreach (XmlSchema s in (await XmlSchemaManager.GetSchemaSet(token)).Schemas())
                {
                    if (s.TargetNamespace != schema.TargetNamespace)
                    {
                        sset.Add(schema);
                    }
                }
                sset.Compile();
            }
            catch (XmlSchemaException ex) {
                errors.Add(CreateBuildError(ex, filename));
            }
            catch (XmlException ex) {
                errors.Add(CreateBuildError(ex, filename));
            }

            return(errors);
        }
Example #2
0
        /// <summary>
        /// Validates the xml against known schemas.
        /// </summary>
        public static async Task <List <BuildError> > ValidateXml(string xml, string filename, CancellationToken token)
        {
            var errors   = new List <BuildError> ();
            var settings = CreateValidationSettings(errors, filename);

            try {
                settings.Schemas = await XmlSchemaManager.GetSchemaSet(token);

                using (var reader = XmlReader.Create(new StringReader(xml), settings)) {
                    while (!reader.EOF)
                    {
                        reader.Read();
                    }
                }
            } catch (XmlSchemaException ex) {
                errors.Add(CreateBuildError(ex, filename));
            }
            catch (XmlException ex) {
                errors.Add(CreateBuildError(ex, filename));
            }

            return(errors);
        }