Created
March 29, 2016 16:31
-
-
Save cpoDesign/8781c3f67f3c17294da0 to your computer and use it in GitHub Desktop.
Implementation of XmlSchema Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Reflection; | |
namespace XmlSchemaValidator.FileUtility | |
{ | |
public class FileHelpers | |
{ | |
public static string AssemblyDirectory | |
{ | |
get | |
{ | |
string codeBase = Assembly.GetExecutingAssembly().CodeBase; | |
UriBuilder uri = new UriBuilder(codeBase); | |
string path = Uri.UnescapeDataString(uri.Path); | |
return Path.GetDirectoryName(path); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace XmlSchemaValidator.FileUtility | |
{ | |
public class FileValidator | |
{ | |
public static bool CheckFileExists(string xmlFilePath) | |
{ | |
if (File.Exists(xmlFilePath)) return true; | |
Console.WriteLine("File does not exists in path: {0}", xmlFilePath); | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace XmlSchemaValidator.XmlValidator | |
{ | |
public interface IXmlSchemaValidator | |
{ | |
void SetXmlFilePath(string xmlFilePath); | |
void SetXsdFilePath(string xsdFilePath); | |
/// <summary> | |
/// Set schema name | |
/// </summary> | |
/// <example> | |
/// Namespace: xmlns:xs="http://www.w3.org/2001/XMLSchema" | |
/// Translates into: http://www.w3.org/2001/XMLSchema | |
/// </example> | |
/// <param name="nsSchema">set usually in xmlns element (http://www.w3.org/2001/XMLSchema)</param> | |
void SetXmlSchema(string nsSchema); | |
void Validate(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string xmlFilePath = Path.Combine(FileHelpers.AssemblyDirectory, "xmlfile.xml"); | |
var xsdFilePath = Path.Combine(FileHelpers.AssemblyDirectory,"definition.xsd"); | |
IXmlSchemaValidator validator = new SchemaValidator(); | |
validator.SetXmlFilePath(xmlFilePath); | |
validator.SetXsdFilePath(xsdFilePath); | |
const string nsSchema = "http://www.test.cominternal"; | |
validator.SetXmlSchema(nsSchema); | |
validator.Validate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Schema; | |
using XmlSchemaValidator.FileUtility; | |
namespace XmlSchemaValidator.XmlValidator | |
{ | |
public class SchemaValidator : IXmlSchemaValidator | |
{ | |
private string _xmlFilePath; | |
private string _xsdFilePath; | |
private string _nsSchema; | |
public void SetXmlFilePath(string xmlFilePath) | |
{ | |
if (!FileValidator.CheckFileExists(xmlFilePath)) throw new FileNotFoundException("Cannot find the file", xmlFilePath); | |
_xmlFilePath = xmlFilePath; | |
} | |
public void SetXsdFilePath(string xsdFilePath) | |
{ | |
if (!FileValidator.CheckFileExists(xsdFilePath)) throw new FileNotFoundException("Cannot find the file", xsdFilePath); | |
_xsdFilePath = xsdFilePath; | |
} | |
public void SetXmlSchema(string nsSchema) | |
{ | |
if (string.IsNullOrWhiteSpace(nsSchema)) throw new ArgumentException("Please provide schema for your xml", "nsSchema"); | |
_nsSchema = nsSchema; | |
} | |
public void Validate() | |
{ | |
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); | |
xmlReaderSettings.Schemas.Add(_nsSchema, this._xsdFilePath); | |
xmlReaderSettings.ValidationType = ValidationType.Schema; | |
xmlReaderSettings.Async = true; | |
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(ValidateSchemaForXml); | |
XmlReader books = XmlReader.Create(_xmlFilePath, xmlReaderSettings); | |
while (books.ReadAsync().Result) | |
{ | |
} | |
} | |
private void ValidateSchemaForXml(object sender, ValidationEventArgs e) | |
{ | |
Console.BackgroundColor = ConsoleColor.Yellow; | |
if (e.Severity == XmlSeverityType.Warning) | |
{ | |
Console.Write("WARNING: "); | |
Console.WriteLine(e.Message); | |
} | |
else if (e.Severity == XmlSeverityType.Error) | |
{ | |
Console.BackgroundColor = ConsoleColor.Red; | |
Console.Write("ERROR: "); | |
Console.WriteLine(e.Message); | |
} | |
Console.BackgroundColor = ConsoleColor.White; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment