Skip to content

Instantly share code, notes, and snippets.

@kmckelvin
Created August 10, 2010 19:40
Show Gist options
  • Save kmckelvin/517852 to your computer and use it in GitHub Desktop.
Save kmckelvin/517852 to your computer and use it in GitHub Desktop.
public bool Validate(XmlSchema schema, string filePath)
{
bool isValid = true;
XmlReaderSettings settings = new XmlReaderSettings();
try
{
settings.Schemas.Add(schema);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += delegate(object sender, ValidationEventArgs e)
{
if (e.Exception != null)
{
isValid = false;
}
};
}
catch (Exception ex)
{
_log.Error("Error occured when loading the XML Schema Definition", ex);
}
XmlReader reader = XmlReader.Create(filePath, settings);
try
{
while(reader.Read());
}
catch
{
isValid = false;
}
finally
{
reader.Close();
}
return isValid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment