Microsoft.Xades.XadesSignedXml.ValidateAgainstSchema C# (CSharp) Method

ValidateAgainstSchema() public method

Validate the XML representation of the signature against the XAdES and XMLDSIG schemas
public ValidateAgainstSchema ( ) : bool
return bool
        public virtual bool ValidateAgainstSchema()
        {
            bool retValue = false;

            Assembly assembly = Assembly.GetExecutingAssembly();
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            XmlSchema xmlSchema;
            Stream schemaStream;

            NameTable xadesNameTable;
            XmlNamespaceManager xmlNamespaceManager;
            XmlParserContext xmlParserContext;

            this.validationErrorOccurred = false;
            this.validationErrorDescription = "";

            try
            {
                schemaStream = assembly.GetManifestResourceStream("Microsoft.Xades.xmldsig-core-schema.xsd");
                xmlSchema = XmlSchema.Read(schemaStream, new ValidationEventHandler(this.SchemaValidationHandler));
                schemaSet.Add(xmlSchema);
                schemaStream.Close();

                schemaStream = assembly.GetManifestResourceStream("Microsoft.Xades.XAdES.xsd");
                xmlSchema = XmlSchema.Read(schemaStream, new ValidationEventHandler(this.SchemaValidationHandler));
                schemaSet.Add(xmlSchema);
                schemaStream.Close();

                if (this.validationErrorOccurred)
                {
                    throw new CryptographicException("Schema read validation error: " + this.validationErrorDescription);
                }
            }
            catch (Exception exception)
            {
                throw new CryptographicException("Problem during access of validation schemas", exception);
            }

            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(this.XmlValidationHandler);
            xmlReaderSettings.ValidationType = ValidationType.Schema;
            xmlReaderSettings.Schemas = schemaSet;
            xmlReaderSettings.ConformanceLevel = ConformanceLevel.Auto;

            xadesNameTable = new NameTable();
            xmlNamespaceManager = new XmlNamespaceManager(xadesNameTable);
            xmlNamespaceManager.AddNamespace("xsd", XadesSignedXml.XadesNamespaceUri);

            xmlParserContext = new XmlParserContext(null, xmlNamespaceManager, null, XmlSpace.None);

            XmlTextReader txtReader = new XmlTextReader(this.GetXml().OuterXml, XmlNodeType.Element, xmlParserContext);
            XmlReader reader = XmlReader.Create(txtReader, xmlReaderSettings);
            try
            {
                while (reader.Read()) ;
                if (this.validationErrorOccurred)
                {
                    throw new CryptographicException("Schema validation error: " + this.validationErrorDescription);
                }
            }
            catch (Exception exception)
            {
                throw new CryptographicException("Schema validation error", exception);
            }
            finally
            {
                reader.Close();
            }

            retValue = true;

            return retValue;
        }