XML schema validations added

pull/460/merge^2
Buddhika Chathuranga 2020-03-29 21:40:26 +05:30
parent 74d1a540dc
commit b38a664128
1 changed files with 34 additions and 0 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using winsw.Configuration;
using winsw.Native;
using winsw.Util;
@ -72,6 +73,15 @@ namespace winsw
throw new InvalidDataException(e.Message, e);
}
try
{
ValidateXmlSchema();
}
catch (Exception e)
{
throw e;
}
// register the base directory as environment variable so that future expansions can refer to this.
Environment.SetEnvironmentVariable("BASE", d.FullName);
@ -95,6 +105,30 @@ namespace winsw
this.dom = dom;
}
private void ValidateXmlSchema()
{
XmlReaderSettings booksettings = new XmlReaderSettings();
booksettings.Schemas.Add(null, BasePath + ".xsd");
booksettings.ValidationType = ValidationType.Schema;
booksettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
XmlReader books = XmlReader.Create(BasePath + ".xml");
books.Read();
}
private void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
throw new Exception("Warning in validation");
}
else if (e.Severity == XmlSeverityType.Error)
{
throw new Exception("Error in validation");
}
}
// ReSharper disable once InconsistentNaming
public static ServiceDescriptor FromXML(string xml)
{