I need to capture any exceptions thrown by the validator but I cannot figure out how to due it. Here is the code I have tried:
internal static class XMLValidator
{
public static void Validate(XElement elem)
{
string xsdMarkup;
using(var file = new StreamReader(Constants.Xsd))
{
xsdMarkup = file.ReadToEnd();
}
XmlSchemaSet schema = new XmlSchemaSet();
bool valid = true;
schema.Add(XmlSchema.Read(XElement.Parse(xsdMarkup).CreateReader(), (o, e) => { }));
new XDocument(elem).Validate(schema, (o, e) => { valid = false; exception = e; });
if (valid == false)
{
throw exception;
}
valid = true;
}
}
I get a “the name exception does not exist in current context” error. I’m pretty sure that the problem is that I have not given exception a data type. However I have no idea what type to use.
I tried adding var before the exception but then it’s not recogonized inside of the if statement and of course var cannot be declared outside of a method
I then tried declaring exemption globally as a string and setting to e like this:
exception = e.ToString();
but then I can’t throw it inside of the if statement.
How would I go about doing this?
The delegate you’re using creates an
XmlSchemaValidationException: