RealWorldDevelopers
4/13/2016 - 10:55 PM

Xml Validator Class

Xml Validator Class


using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace xmlSandbox.cs.Business
{

    public interface IXmlXsdValidator
    {
        /// <summary>
        /// Validate a Collection of <see cref="XmlDocument"/> by using a for each loop. <seealso cref="IsValid(XmlDocument)"/>
        /// </summary>
        /// <param name="xmlDocs">XML to be validated as List of <see cref="XmlDocument"/></param>
        /// <returns></returns>
        bool IsValid(List<XmlDocument> xmlDocs);
        /// <summary>
        /// Validate a Collection of <see cref="XDocument"/> by using a for each loop. <seealso cref="IsValid(XDocument)"/>
        /// </summary>
        /// <param name="xDocs">XML to be validated as List of <see cref="XDocument"/></param>
        /// <returns></returns>
        bool IsValid(List<XDocument> xDocs);
        /// <summary>
        /// Validate a <see cref="XDocument"/> by using a for each loop. <seealso cref="IsValid(XDocument)"/>
        /// </summary>
        /// <param name="xDoc">XML to be validated as <see cref="XDocument"/></param>
        /// <returns></returns>
        bool IsValid(XDocument xDoc);
        /// <summary>
        /// Validate a <see cref="XmlDocument"/> by using a for each loop. <seealso cref="IsValid(XmlDocument)"/>
        /// </summary>
        /// <param name="xmlDoc">XML to be validated as <see cref="XmlDocument"/></param>
        /// <returns></returns>
        bool IsValid(XmlDocument xmlDoc);
        event XmlXsdValidator.ValidationErrorEventHandler ValidationErrorEvent;
    }

    /// <summary>
    /// Used to Validate a <see cref="XDocument"/> against a <see cref="XmlSchemaSet"/> 
    /// </summary>
    public class XmlXsdValidator : IXmlXsdValidator
    {

        /// <summary>
        /// Contructor for Validation Tool.  
        /// A Schema Set must be passed in.
        /// </summary>
        /// <param name="xmlSchemaSet"><see cref="XmlSchemaSet"/> to use for Validation</param>
        public XmlXsdValidator(XmlSchemaSet xmlSchemaSet)
        {
            _schemas = xmlSchemaSet;
        }


        /// <summary>
        /// Modular <see cref="XmlSchemaSet"/> to be used for all validation.
        /// </summary>
        private readonly XmlSchemaSet _schemas;


        public delegate void ValidationErrorEventHandler(object sender, ValidationEventArgs e);



        public event ValidationErrorEventHandler ValidationErrorEvent;


        /// <summary>
        /// The OnValidationErrorEvent is fired with each xsd validation failure.
        /// </summary>
        /// <param name="xDoc">This is the XML that failed <seealso cref="IsValid(XDocument)"/>validation as <see cref="XDocument"/></param>
        /// <param name="e">This is validation errors information as <see cref="ValidationEventArgs"/></param>
        protected virtual void OnValidationErrorEvent(object xDoc, ValidationEventArgs e)
        {
            if (ValidationErrorEvent != null)
            {
                ValidationErrorEvent(xDoc, e);
            }
        }


        public bool IsValid(List<XmlDocument> xmlDocs)
        {
            bool errors = false;
            foreach (var xmlDoc in xmlDocs)
            {
                if (IsValid(xmlDoc))
                    errors = true;
            }
            return errors == false;
        }

        /// <inheritdoc/>
        public bool IsValid(List<XDocument> xDocs)
        {
            bool errors = false;
            foreach (var xDoc in xDocs)
            {
                if (IsValid(xDoc))
                    errors = true;
            }
            return errors == false;
        }

        /// <inheritdoc/>
        public bool IsValid(XDocument xDoc)
        {

            var errors = false;
            xDoc.Validate(_schemas, (sender, args) =>
            {
                OnValidationErrorEvent(xDoc, args);
                errors = true;
            });
            return errors == false;
        }

        /// <inheritdoc/>
        public bool IsValid(XmlDocument xmlDoc)
        {
            return IsValid(xmlDoc.ToXDocument());
        }

    }
}