first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# Code in Cold Storage: A Schema-Based Data Input Wrapper for XML Input

using Songhay.Xml; using System; using System.IO; using System.Xml; using System.Xml.Schema;

namespace Songhay.Data { /// <summary> /// Data input wrapper for XML input. /// </summary> public abstract class XmlDataInput { /// <summary> /// When <c>true</c> the constructor input is valid. /// </summary> public bool IsValid { get { return this._isValid; } set { this._isValid = value; } }

    /// &lt;summary&gt;
    /// Validation report.
    /// &lt;/summary&gt;
    public string ValidationMessage
    {
        get { return this._validationMessage; }
        set { this._validationMessage = value; }
    }

    /// &lt;summary&gt;
    /// Required validation implementation.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;xpDoc&quot;&gt;
    /// The &lt;see cref=&quot;System.Xml.XPath.XPathDocument&quot;/&gt; to validate.
    /// &lt;/param&gt;
    protected void Validate(String XmlPostData, String SchemaPath)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.CheckCharacters = true;
        settings.ValidationType = ValidationType.Schema;
        settings.Schemas.Add(null,SchemaPath);
        settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler +=
            new ValidationEventHandler(XmlValidationEventHandler);

        this.ValidationMessage = String.Empty;
        this._isValid = true;

        XmlReader reader =
            XmlReader.Create(new StringReader(XmlPostData),settings);
    }

    private void XmlValidationEventHandler(object sender, ValidationEventArgs e)
    {
        this._validationMessage =
            String.Format(&quot;{0}{1}{2}{1}&quot;,
                this._validationMessage,Environment.NewLine,e.Message);

        if (e.Severity == XmlSeverityType.Error)
        {
            this._isValid = false;

            if(e.Exception != null)
            {
                this._validationMessage =
                    String.Format(&quot;{0}{1}{1}Exception:{1}&quot;,
                        this._validationMessage,Environment.NewLine);
                this._validationMessage =
                    String.Format(&quot;{0}{1}    Line Number:{2}{1}&quot;,
                        this._validationMessage, Environment.NewLine, e.Exception.LineNumber);
                this._validationMessage =
                    String.Format(&quot;{0}{1}    Line Position:{2}{1}&quot;,
                        this._validationMessage, Environment.NewLine, e.Exception.LinePosition);
                this._validationMessage =
                    String.Format(&quot;{0}{1}    Message:{2}{1}&quot;,
                        this._validationMessage, Environment.NewLine, e.Exception.Message);
            }
        }
    }

    private bool _isValid;
    private string _validationMessage;
}

}

mod date: 2006-04-25T19:22:38.000Z