Class to Validate XML Against XSD


Recently i was involved in programming generic interface for Submitting PO. I came across the XSD concept of programming. I used the XSD to validate xml passed from different sub system and serialize the data.

Below is the sample code to validate the Xml against Xsd

/////////////////////////////

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

namespace XSDValidation
{
    public static class POXsdValidation
    {
        private static string ErrorMessage;

public enum XmlType
    {
        XSD1,
        XSD2
    }

        public static string XsdError
        {
            get
            {
                return ErrorMessage;
            }
        }
        /// <summary>
        /// Validates the xml string against the xsd
        /// </summary>
        /// <param name="InputXml">Xml String contains the full xml</param>
        /// <param name="XsdPath">Xsd Path to be validated</param>
        private static void ValidateXmlByPath(string InputXml,string XsdPath)
        {
            StringReader xmlString = new StringReader(InputXml);
            ////StringReader xsdReader = new StringReader(XsdPath);
            ErrorMessage = string.Empty;
            ////XmlReader XsdReader = XmlReader.Create(xsdReader);

            //// Create the XmlSchemaSet class.
            XmlSchemaSet sc = new XmlSchemaSet();

            //// Add the schema to the collection.
            sc.Add(null, XsdPath);

            //// Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = sc;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            //// Create the XmlReader object.
            XmlReader reader = XmlReader.Create(xmlString, settings);

            //// Parse the file.
            while (reader.Read())
            {
            }
        }
        /// <summary>
        /// Validates the xmlreader content against xsd
        /// </summary>
        /// <param name="InputXml">Xml Reader</param>
        /// <param name="XsdPath">XSD Path</param>
        private static void ValidateXml(XmlReader InputXml, string XsdPath)
        {
            ////StringReader xmlString = new StringReader(InputXml);
            ////StringReader xsdReader = new StringReader(XsdPath);
            ErrorMessage = string.Empty;
            ////XmlReader XsdReader = XmlReader.Create(xsdReader);

            //// Create the XmlSchemaSet class.
            XmlSchemaSet sc = new XmlSchemaSet();

            //// Add the schema to the collection.
            sc.Add(null, XsdPath);

            //// Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = sc;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            //// Create the XmlReader object.
            XmlReader reader = XmlReader.Create(InputXml, settings);

            //// Parse the file.
            while (reader.Read())
            {
            }
        }
        /// <summary>
        /// Validates the Xml Against xsd
        /// </summary>
        /// <param name="InputXml">Input Xml String to Validate</param>
        /// <param name="InputType">Type of Validation</param>
        public static void ValidateXml(string InputXml,XmlType InputType)
        {
            string PathWCF = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
            //// Add the schema to the collection.
            switch (InputType)
            {
                case XML1:
                    PathWCF += "\\bin\\schemas\\XSD1.xsd";
                    break;
                case XML2:
                    PathWCF += "\\bin\\schemas\\XSD2.xsd";
                    break;
            }

            ValidateXmlByPath(InputXml, PathWCF);
        }
        /// <summary>
        /// Callback function for Validate Schema
        /// </summary>
        /// <param name="sender">Object value</param>
        /// <param name="e">Validation message object</param>
        private static void ValidationCallBack(object sender, ValidationEventArgs e)
        {
            ErrorMessage += "<Validation>" +  e.Message + "</Validation>" ;
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s