How to throw a XmlSchemaException on XML Schema validation errors?
Question
Calling Validate()
on an XmlDocument requires passing in a ValidationEventHandler
delegate. That event function gets a ValidationEventArgs
parameter which in turn has an Exception
property of the type XmlSchemaException
. Whew!
My current code looks like this:
ValidationEventHandler onValidationError = delegate(object sender,
ValidationEventArgs args)
{
throw(args.Exception);
}
doc.Validate(onValidationError);
Is there some other method I'm overlooking which simply throws the XmlSchemaException
if validation fails (warnings ignored entirely)?
Solution
Because the Validate method takes the ValidationEventHandler delegate, it is left up to the developer to decide what to do with the excpetion. What you are doing is correct.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow