/// <summary> /// Receives notification of a validation error. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void ValidationHandler(object sender, ValidationEventArgs args) { //store the validation error message and set result flag if (args.Severity == XmlSeverityType.Warning) { AddToArrayList(ref m_warnings, args.Message); if (m_result == ValidatorResultType.Valid) { m_result = ValidatorResultType.ValidWithWarnings; } } else { AddToArrayList(ref m_errors, args.Message); m_result = ValidatorResultType.InValid; } }
/// <summary> /// Receives notification of a validation error. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void ValidationHandler(object sender, ValidationEventArgs args) { //store the validation error message and set result flag if (args.Severity == XmlSeverityType.Warning) { AddToArrayList(ref m_warnings, args.Message); if (m_result == ValidatorResultType.Valid) m_result = ValidatorResultType.ValidWithWarnings; } else { AddToArrayList(ref m_errors, args.Message); m_result = ValidatorResultType.InValid; } }
/// <summary> /// Validates XML against a schema. /// </summary> /// <param name="xml">XML to validate.</param> /// <param name="shortCircuit"> /// Defines validation behaviour should invalid XML be encountered. Setting to a type other than None /// forces the validation process to immedietely quit according to the type of problem encountered. /// </param> /// <returns>Result as ValidatorResultType. Extra information can be extracted from Warnings and Errors properties.</returns> /// <overloaded/> public ValidatorResultType Validate(string xml, ValidatorShortCircuitType shortCircuit) { // NB overloaded //validate arguments if (xml == null) throw new ArgumentNullException("xml"); else if (xml.Length == 0) throw new ArgumentOutOfRangeException("xml"); //initialise variables m_warnings = null; m_errors = null; m_result = ValidatorResultType.Valid; try { //load the xml into validating reader XmlValidatingReader xmlValidatingReader = null; //configure validating reader and schema(s) according to schema type if (m_validationType == ValidationType.Schema) { //if the XSD schema collection is not yet prepared, do so now if (!m_isSchemaCollectionPrepared) { //prepare the additional resources resolver with all of the extra XSD's PrepareResourceResolver(); //load main XSD into schemas collection m_schemas = new XmlSchemaCollection(); m_schemas.Add(null, GetXmlTextReader(m_schema), m_additionalResourceResolver); //flag that we have already prepared the schemas to improve //performance on subsequent validation requests m_isSchemaCollectionPrepared = true; } //create the validating reader and assign schemas xmlValidatingReader = new XmlValidatingReader(GetXmlTextReader(xml)); xmlValidatingReader.ValidationType = ValidationType.Schema; xmlValidatingReader.Schemas.Add(m_schemas); } else { //set up validating reader for DTD XmlParserContext context = new XmlParserContext(null, null, m_dtdDocTypeName, null, null, m_schema, "", "", XmlSpace.None); xmlValidatingReader = new XmlValidatingReader(xml, XmlNodeType.Element, context); xmlValidatingReader.ValidationType = ValidationType.DTD; xmlValidatingReader.EntityHandling = EntityHandling.ExpandCharEntities; } //assign event handler which will receive validation errors xmlValidatingReader.ValidationEventHandler += new ValidationEventHandler (ValidationHandler); //read the xml to perform validation try { while (xmlValidatingReader.Read()) { //check if we need to quit if (shortCircuit == ValidatorShortCircuitType.OnWarning && (m_result == ValidatorResultType.ValidWithWarnings || m_result == ValidatorResultType.InValid)) break; else if (shortCircuit == ValidatorShortCircuitType.OnError && m_result == ValidatorResultType.InValid) break; } } catch (Exception exc) { //xml form error AddToArrayList(ref m_errors, exc.Message); m_result = ValidatorResultType.InValid; } return m_result; } catch (Exception exc) { throw exc; } }
/// <summary> /// Validates XML against a schema. /// </summary> /// <param name="xml">XML to validate.</param> /// <param name="shortCircuit"> /// Defines validation behaviour should invalid XML be encountered. Setting to a type other than None /// forces the validation process to immedietely quit according to the type of problem encountered. /// </param> /// <returns>Result as ValidatorResultType. Extra information can be extracted from Warnings and Errors properties.</returns> /// <overloaded/> public ValidatorResultType Validate(string xml, ValidatorShortCircuitType shortCircuit) { // NB overloaded //validate arguments if (xml == null) { throw new ArgumentNullException("xml"); } else if (xml.Length == 0) { throw new ArgumentOutOfRangeException("xml"); } //initialise variables m_warnings = null; m_errors = null; m_result = ValidatorResultType.Valid; try { //load the xml into validating reader XmlValidatingReader xmlValidatingReader = null; //configure validating reader and schema(s) according to schema type if (m_validationType == ValidationType.Schema) { //if the XSD schema collection is not yet prepared, do so now if (!m_isSchemaCollectionPrepared) { //prepare the additional resources resolver with all of the extra XSD's PrepareResourceResolver(); //load main XSD into schemas collection m_schemas = new XmlSchemaCollection(); m_schemas.Add(null, GetXmlTextReader(m_schema), m_additionalResourceResolver); //flag that we have already prepared the schemas to improve //performance on subsequent validation requests m_isSchemaCollectionPrepared = true; } //create the validating reader and assign schemas xmlValidatingReader = new XmlValidatingReader(GetXmlTextReader(xml)); xmlValidatingReader.ValidationType = ValidationType.Schema; xmlValidatingReader.Schemas.Add(m_schemas); } else { //set up validating reader for DTD XmlParserContext context = new XmlParserContext(null, null, m_dtdDocTypeName, null, null, m_schema, "", "", XmlSpace.None); xmlValidatingReader = new XmlValidatingReader(xml, XmlNodeType.Element, context); xmlValidatingReader.ValidationType = ValidationType.DTD; xmlValidatingReader.EntityHandling = EntityHandling.ExpandCharEntities; } //assign event handler which will receive validation errors xmlValidatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); //read the xml to perform validation try { while (xmlValidatingReader.Read()) { //check if we need to quit if (shortCircuit == ValidatorShortCircuitType.OnWarning && (m_result == ValidatorResultType.ValidWithWarnings || m_result == ValidatorResultType.InValid)) { break; } else if (shortCircuit == ValidatorShortCircuitType.OnError && m_result == ValidatorResultType.InValid) { break; } } } catch (Exception exc) { //xml form error AddToArrayList(ref m_errors, exc.Message); m_result = ValidatorResultType.InValid; } return(m_result); } catch (Exception exc) { throw exc; } }