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; } }
/// <summary>
/// Validation report.
/// </summary>
public string ValidationMessage
{
get { return this._validationMessage; }
set { this._validationMessage = value; }
}
/// <summary>
/// Required validation implementation.
/// </summary>
/// <param name="xpDoc">
/// The <see cref="System.Xml.XPath.XPathDocument"/> to validate.
/// </param>
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("{0}{1}{2}{1}",
this._validationMessage,Environment.NewLine,e.Message);
if (e.Severity == XmlSeverityType.Error)
{
this._isValid = false;
if(e.Exception != null)
{
this._validationMessage =
String.Format("{0}{1}{1}Exception:{1}",
this._validationMessage,Environment.NewLine);
this._validationMessage =
String.Format("{0}{1} Line Number:{2}{1}",
this._validationMessage, Environment.NewLine, e.Exception.LineNumber);
this._validationMessage =
String.Format("{0}{1} Line Position:{2}{1}",
this._validationMessage, Environment.NewLine, e.Exception.LinePosition);
this._validationMessage =
String.Format("{0}{1} Message:{2}{1}",
this._validationMessage, Environment.NewLine, e.Exception.Message);
}
}
}
private bool _isValid;
private string _validationMessage;
}
}