XmlDocument.Validate() - get error message from exception



Is it possible to return the errors from Xml.Validate(...)? i.e. the point at which my xml fails to validate against the xsd.


In this snippet the validation would simply fail the try-catch, and return false. Removing the try-catch throws a system exception.


Note: 'var Xml' is of type XmlDocument.



public static class XmlValidator
{
public static bool Validate(UploadedFile uploadedFile)
{
try
{
var Xml = uploadedFile.XmlFromUpload();
string XsdPath = @"C:\Projects\XMLValidator\Xsd\books.xsd";

Xml.Schemas.Add(null, XsdPath);
Xml.Validate(ValidationCallBack);
return true;
}
catch
{
return false;
}
}

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
throw new Exception();
}
}

No comments:

Post a Comment